Problem Solving with C++, Student Value Edition plus MyProgrammingLab with Pearson eText -- Access Card Package (9th Edition)
9th Edition
ISBN: 9780133862225
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 18, Problem 2P
Program Plan Intro
Histogram of positive numbers
Program Plan:
- Include required header file.
- Include required “std” namespace.
- Define main function
- Declare a variable in “map” template class.
- Initializes the variable “numbers” to “0”.
- Display prompt statement.
- Performs “while” loop. This loop will perform upto “numbers” equal to “-1”.
- In this loop, first read the number from user.
- If the number is not equal to “-1”, then check if the number is in map or not using “find” function.
- If it is, then assign “mapResult[numbers]” is “1”.
- Otherwise, increment the counter
- Declare constant iter “result” in “map” template class.
- Display the given result using “for” loop.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Please write a full C++ code and provide code and output
Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class linkedStackTypethat returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator.
Write a function that gets a linked list of ints, and reverses it. For example
- on input 1 -> 2 -> 3 -> 4, after the function finishes the execution, the list
becomes 4 -> 3 -> 2 -> 1
- If the list has one element, then it doesn’t change
- If the list is empty, then it doesn’t change
You may use the data fields in the struct and the functions provided in LL.h and LL.c.
// reverses a linked list
void LL_reverse(LL_t* list);
Test for the Function:
void test_q3() {
LL_t* lst = LLcreate();
LL_add_to_tail(lst, 1);
LL_add_to_tail(lst, 3);
LL_add_to_tail(lst, 8);
LL_add_to_tail(lst, 4);
LL_add_to_tail(lst, 3);
LL_reverse(lst);
intcorrect[] = {3,4,8,3,1};
inti;
node_t* n = lst->head;
for(i=0;i<5;i++) {
if (n==NULL) {
printf("Q3 ERROR: node %d==NULL unexpected\n", i);
return;
}
if (n->data != correct[i]) {
printf("Q3 ERROR: node%d->data==%d, expected %d\n", i, n->data, correct[i]);
return;
}
n = n->next;
}
if (n==NULL)
printf("Q3 ok\n");
}
Support…
Write a function that gets a linked list of ints, and reverses it. For example
- on input 1 -> 2 -> 3 -> 4, after the function finishes the execution, the list
becomes 4 -> 3 -> 2 -> 1
- If the list has one element, then it doesn’t change
- If the list is empty, then it doesn’t change
You may use the data fields in the struct and the functions provided in LL.h and LL.c.
// reverses a linked list
void LL_reverse(LL_t* list);
Test for the Function;
void test_q3() {
LL_t* lst = LLcreate();
LL_add_to_tail(lst, 1);
LL_add_to_tail(lst, 3);
LL_add_to_tail(lst, 8);
LL_add_to_tail(lst, 4);
LL_add_to_tail(lst, 3);
LL_reverse(lst);
intcorrect[] = {3,4,8,3,1};
inti;
node_t* n = lst->head;
for(i=0;i<5;i++) {
if (n==NULL) {
printf("Q3 ERROR: node %d==NULL unexpected\n", i);
return;
}
if (n->data != correct[i]) {
printf("Q3 ERROR: node%d->data==%d, expected %d\n", i, n->data, correct[i]);
return;
}
n = n->next;
}
if (n==NULL)
printf("Q3 ok\n");
}
Support File…
Chapter 18 Solutions
Problem Solving with C++, Student Value Edition plus MyProgrammingLab with Pearson eText -- Access Card Package (9th Edition)
Ch. 18.1 - If v is a vector, what does v.begin() return? What...Ch. 18.1 - If p is an iterator for a vector object v, what is...Ch. 18.1 - Suppose v is a vector of ints. Write a for loop...Ch. 18.1 - Suppose the vector v contains the letters 'A',...Ch. 18.1 - Suppose the vector v contains the letters 'A',...Ch. 18.1 - Suppose you want to run the following code, where...Ch. 18.2 - Prob. 7STECh. 18.2 - Prob. 8STECh. 18.2 - Prob. 9STECh. 18.2 - Prob. 10STE
Ch. 18.2 - Prob. 11STECh. 18.2 - Prob. 12STECh. 18.2 - Prob. 13STECh. 18.2 - Prob. 14STECh. 18.2 - Prob. 15STECh. 18.2 - Prob. 16STECh. 18.3 - Prob. 17STECh. 18.3 - Prob. 18STECh. 18.3 - Prob. 19STECh. 18.3 - Suppose v is an object of the class vectorint. Use...Ch. 18.3 - Prob. 21STECh. 18.3 - Can you use the copy template function with vector...Ch. 18.3 - Prob. 23STECh. 18 - Prob. 1PCh. 18 - Prob. 2PCh. 18 - Prob. 3PCh. 18 - Prob. 4PCh. 18 - Write a program that allows the user to enter any...Ch. 18 - Prob. 3PPCh. 18 - Prob. 5PPCh. 18 - Solution to Programming Project 18.6 In this...Ch. 18 - Prob. 7PPCh. 18 - You have collected a file of movie ratings where...Ch. 18 - Prob. 9PP
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- This is in c++ Given the MileageTrackerNode class, complete main() to insert nodes into a linked list (using the InsertAfter() function). The first user-input value is the number of nodes in the linked list. Use the PrintNodeData() function to print the entire linked list. Don't print the dummy head node. Example input : 3 2.2 7/2/18 3.2 7/7/18 4.5 7/16/18 output: 2.2, 7/2/18 3.2, 7/7/18 4.5, 7/16/18 main.cpp: #include "MileageTrackerNode.h"#include <string>#include <iostream>using namespace std; int main (int argc, char* argv[]) {// References for MileageTrackerNode objectsMileageTrackerNode* headNode;MileageTrackerNode* currNode;MileageTrackerNode* lastNode; double miles;string date;int i; // Front of nodes listheadNode = new MileageTrackerNode();lastNode = headNode; // TODO: Read in the number of nodes // TODO: For the read in number of nodes, read// in data and insert into the linked list // TODO: Call the PrintNodeData() method// to print the entire linked list //…arrow_forwardThis is in c++ Given the MileageTrackerNode class, complete main() to insert nodes into a linked list (using the InsertAfter() function). The first user-input value is the number of nodes in the linked list. Use the PrintNodeData() function to print the entire linked list. Don't print the dummy head node. Example input : 3 2.2 7/2/18 3.2 7/7/18 4.5 7/16/18 output: 2.2, 7/2/18 3.2, 7/7/18 4.5, 7/16/18 main.cpp: #include "MileageTrackerNode.h"#include <string>#include <iostream>using namespace std; int main (int argc, char* argv[]) {// References for MileageTrackerNode objectsMileageTrackerNode* headNode;MileageTrackerNode* currNode;MileageTrackerNode* lastNode; double miles;string date;int i; // Front of nodes listheadNode = new MileageTrackerNode();lastNode = headNode; // TODO: Read in the number of nodes // TODO: For the read in number of nodes, read// in data and insert into the linked list // TODO: Call the PrintNodeData() method// to print the entire linked list //…arrow_forwardThere’s a somewhat imperfect analogy between a linked list and a railroad train, where individual cars represent links. Imagine how you would carry out various linked list operations, such as those implemented by the member functions insertFirst(), removeFirst(), and remove(int key) from the LinkList class in this hour. Also implement an insertAfter() function. You’ll need some sidings and switches. You can use a model train set if you have one. Otherwise, try drawing tracks on a piece of paper and using business cards for train cars.arrow_forward
- Please answer it in Python Write a function pairsbypairslist that would return pair by pair from the smallest value to the biggest value For example [12, 13, 11, 15], # return : (11, 12) and (13, 15) [9, 15, 4, 24, 19, 2] # return : (2,4) (9,15) (19,24) [1, 5, 9, 15, 21, 9, 12, 9], # return : (1,5) (9,9) (9,12) (15,21)arrow_forwardThe goal of this second exercise is to develop a better understanding of the Linuxinterprocess communication mechanisms. This would require you to write twoprograms P1 and P2. The first program P1 needs to generate an array of 50random strings (of characters) of fixed length each. P1 then sends a group offive consecutive elements of the array of strings to P2 along with the ID’s ofthe strings, where the ID is the index of the array corresponding to the string.The second program P2 needs to accept the received strings, and send backthe highest ID received back to P1 to acknowledge the strings received. Theprogram P2 simply prints the ID’s and the strings on the console. On receivingthe acknowledged packet, P1 sends the next five strings, with the string elementsstarting from the successor of the acknowledged ID. The above mechanism needs to be implemented FIFO and UNIX DOMAIN SOCKETS. Please note that you may NOT make assumptions about the reliability of the interprocess communication…arrow_forwardWrite a C++ program that: (1) defines and implements a hash class that constructs a 15 element vector, storing strings, using the following hash function: ((first_letter) + (last_letter) - (second_letter))% 15 So, for example, the word "rocky" would be ('r') = 114 + ('y') = 121 - ('o') = 111 = 124%15 = 4 In this example, an attempt would be made to store "rocky" in position 4. In the event of a collision, the word would be stored in the first available location, so if there is a collision in location 4, an attempt would be made to store the word in location 5. If an attempt to store a word in position 14 fails because the location is full circle back to location 0 because there is no location 15. If there is a collision in location 0, try location 1, and so on. I will not try your program with words that are shorter than three letters, so you do not need to consider shorter words. (2) the driver program should: a. query the user for ten words and store them…arrow_forward
- Implement a __setitem__ function that also supports negative indices. For example: W = L2(Node(10, Node(20, Node(30))))print W[ 10, 20, 30 ] W[1]=25print W[ 10, 25, 30 ] W[-1]=35print W[ 10, 25, 35 ] Complete the code: def L2(*args,**kwargs): class L2_class(L): def __getitem__(self, idx): <... YOUR CODE HERE ...> def __setitem__(self, idx, value): <... YOUR CODE HERE ...> return L2_class(*args,**kwargs) W = L2(Node(10, Node(20, Node(30))))print(W)W[1]=25print(W)W[-1]=35print(W)arrow_forwardWrite a C++ program that: (1) defines and implements a hash class that constructs a 15 element array (may be implemented using a vector, a deque, or a list, if you prefer, (using the STL implementations, not Nyhoff's), storing strings, using the following hash function: ((first_letter) + (last_letter) - (second_letter))% 15 (2) the driver program should: a. query the user for ten words and store them using the hash technique described above. b. print out the contents of each position of the array (or vector, deque, or whatever you used), showing vacant as well as filled positions. Remember, only 10 of the 15 positions will be filled. c. repeatedly query the user for a target word, hash the word, check for its inclusion in the list of stored words, and report the result. Continue doing this task until the user signals to stop (establish a sentinel condition).arrow_forwardImplement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient Greeks. Choose an integer n. This function will compute all prime numbers up to, and including, n. First insert all numbers from 1 to n into a set. Then erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, ... . Erase all multiples of 3, that is, 6, 9, 12, 15, ... . Go up to ?⎯⎯√n. The remaining numbers are all primes. Make sure that you use functions in your solution, including a main function. Define def sieveOfEratosthenes(n) that receives the maximum value and returns a list of all prime numbers less than or equal to this value in main() print the list of prime numbers.arrow_forward
- Consider the following definition of class Person: 1 public class Person 2 { 3 public final String name; 4 5 public Person(String name) 6 { 7 if (name == null) this.name = "<NoName>"; //No null names... 8 else this.name = name; 9 } 10 } and the definition of class PersonListUtils implemented by a student... 1 public class PersonListUtils 2 { 3 //Replaces the first occurrence of a Person in the list with the given oldPersonName with a new Person having the newPersonName. 4 //Returns true if the substitution is successful, false otherwise 5 public static boolean substitute(ArrayList<Person> list, String oldPersonName, String newPersonName) 6 { 7 for (Person person: list) { 8 if (person.name == oldPersonName) { 9 person = new Person(newPersonName); 10 return true; 11 } 12 } 13 return false; 14 } 15 16 //Removes from the list the first occurrence of a Person with the given nameToRemove. 17 //Returns true if the deletion is successful, false otherwise 18 public static boolean…arrow_forwardYou are going to implement a program that creates an unsorted list by using a linked list implemented by yourself. NOT allowed to use LinkedList class or any other classes that offers list functions. It is REQUIRED to use an ItemType class and a NodeType struct to solve this. Use C++, please read instructions carefully and new solution only! The “data.txt” file has three lines of data 100, 110, 120, 130, 140, 150, 160 100, 130, 160 1@0, 2@3, 3@END You need to 1. create an empty unsorted list 2. add the numbers from the first line to list using putItem() function. Then print all the current keys to command line in one line using printAll(). 3. delete the numbers given by the second line in the list by using deleteItem() function. Then print all the current keys to command line in one line using printAll().. 4. putItem () the numbers in the third line of the data file to the corresponding location in the list. For example, 1@0 means adding number 1 at position 0 of the list. Then…arrow_forwardImplement the function below void swap(int pos1, int pos2) {} Inital code to be completed: class LinkedList : public List { node* head; node* tail; int index; node* create_node(int num) { node* n = (node*) malloc(sizeof(node)); n->element = num; n->next = NULL; return n; } public: LinkedList() { index = 0; head = NULL; tail = NULL; } int add(int num) { addTail(num); return index; } int get(int pos) { node* currnode = head; int count =0; while (currnode != NULL){ count++; if (count == pos){ return currnode -> element; }else{ currnode = currnode -> next; } } return -1; } int size() { return index; } void swap(int pos1, int pos2) { } // WARNING!…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning