Question

In: Computer Science

Write a C++ program that will make changes in the list of strings by modifying its...

Write a C++ program that will make changes in the list of strings by modifying its last element.

Your program should have two functions:

1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value.

2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and inserting a new element with the new value.

The main creates the list, creates the string variables, populates few elements in the list (couple is enough) and calls the functions to modify the list. After each call to the functions, the main should print out the value of the last element in the list, so the changes are visible. Again, you print the last element not from within the functions, but from the main.

Note 1: You will need to use references to implement the in-place requirement.

Note 2: However difficult that assignment may look like, all information you need on how to work with the list is given to you in the lecture and both your functions implementation are just very few short statements (hint: If you find yourself writing 5 or more lines of codes for each function, you probably do it wrong).

Please Describe all variables and important sections of code.

Need a screenshot of the out put.

Solutions

Expert Solution

Using List

#include <iostream>

#include <list>
#include <iterator>
#define MAXR 5 // Maximum number of rows
#define MAXC 2 // Maximum number of columns
using namespace std;


// Function to display the list contents
void show(list <string> ins)
{
// Creates an iterator
list <string> :: iterator it;
// Counter for next line
int c = 1;
// Loops till end of the list
for(it = ins.begin(); it != ins.end(); ++it, c++)
// Checks if counter is divisible by 2
// display data with next line
if(c % 2 == 0)
cout <<*it<<endl;
// Otherwise display data with tab space
else
cout <<*it<<"\t";
}// End of function

// Function to replace the last string of given row number as parameter
// with the given newElement string
void toChange(list <string> &ins, string newElement, int row)
{
// Creates an iterator points to beginning
list<string>::iterator it = ins.begin();
// Moves to the position to delete string
advance(it, (row * 2) + 1);
// Delete string at it position
ins.erase(it);

// Move to beginning
it = ins.begin();
// Delete string at it position
advance(it, (row * 2) + 1);
// Insert string at it position
ins.insert(it, 1, newElement);
}// End of function

// Function to accept a string and calls the function for replacement
void toCompare(list <string> &ins, int row)
{
string newElement;
// Accept a string to replace
cout<<"\n Enter the new element: ";
cin>>newElement;
// Calls the function for replacement
toChange(ins, newElement, row);
}// End of function

// main function definition
int main()
{
// Declares a list of type string
list <string> insectList;

// Initializes the matrix
string insects[MAXR][MAXC] =
{
{"Grasshopper", "Ant"},
{"Caterpillar", "Beetle"},
{"Mosquito", "Fly"},
{"Dragonfly", "Bee"},
{"Cockroach", "Spider"},
};// End of initialization

// Loops till number of rows
for(int r = 0; r < MAXR; r++)
// Loops till number of columns
for(int c = 0; c < MAXC; c++)
// Inserts each element of matrix in list
insectList.push_back(insects[r][c]);

// Calls the function to display the matrix
cout<<"\n **************** Original data **************** \n";
show(insectList);

// Loops till number of rows
for(int c = 0; c < MAXR; c++)
{
// Displays message
cout<<"\n After modifying row: "<<(c + 1)<<" last element. \n";
// Calls the function to replace each row last string
// c is used for row number
toCompare(insectList, c);
// Calls the function to display the matrix
show(insectList);
}// End of for loop*/
return 0;
}// End of main function

Sample Output:

Original data
Grasshopper Ant
Caterpillar Beetle
Mosquito Fly
Dragonfly Bee
Cockroach Spider

After modifying row: 1 last element.

Enter the new element: Dog
Grasshopper Dog
Caterpillar Beetle
Mosquito Fly
Dragonfly Bee
Cockroach Spider

After modifying row: 2 last element.

Enter the new element: Lion
Grasshopper Dog
Caterpillar Lion
Mosquito Fly
Dragonfly Bee
Cockroach Spider

After modifying row: 3 last element.

Enter the new element: Ox
Grasshopper Dog
Caterpillar Lion
Mosquito Ox
Dragonfly Bee
Cockroach Spider

After modifying row: 4 last element.

Enter the new element: Panda
Grasshopper Dog
Caterpillar Lion
Mosquito Ox
Dragonfly Panda
Cockroach Spider

After modifying row: 5 last element.

Enter the new element: Squirrel
Grasshopper Dog
Caterpillar Lion
Mosquito Ox
Dragonfly Panda
Cockroach Squirrel

Using Matrix

#include <iostream>
#include <string>
#define MAXR 5 // Maximum number of rows
#define MAXC 2 // Maximum number of columns
using namespace std;

// Function to display the matrix contents
void show(string ins[][MAXC])
{
// Loops till number of rows
for(int r = 0; r < MAXR; r++)
{
// Loops till number of columns
for(int c = 0; c < MAXC; c++)
// Displays each elements
cout<<ins[r][c]<<"\t";
cout<<endl;
}// End of for loop
}// End of function

// Function to replace the last string of given row number as parameter
// with the given newElement string
void toChange(string ins[][MAXC], string newElement, int row)
{
// Replaces string at last index position of row number passed as parameter
ins[row][MAXC-1] = newElement;
}// End of function

// Function to accept a string and calls the function for replacement
void toCompare(string ins[][MAXC], int row)
{
string newElement;
// Accept a string to replace
cout<<"\n Enter the new element: ";
cin>>newElement;
// Calls the function for replacement
toChange(ins, newElement, row);
}// End of function

// main function definition
int main()
{
// Initializes the matrix
string insects[MAXR][MAXC] =
{
{"Grasshopper", "Ant"},
{"Caterpillar", "Beetle"},
{"Mosquito", "Fly"},
{"Dragonfly", "Bee"},
{"Cockroach", "Spider"},
};// End of initialization

// Calls the function to display the matrix
cout<<"\n Original data \n";
show(insects);

// Loops till number of rows
for(int c = 0; c < MAXR; c++)
{
// Displays message
cout<<"\n After modifying row: "<<(c + 1)<<" last element. \n";
// Calls the function to replace each row last string
// c is used for row number
toCompare(insects, c);
// Calls the function to display the matrix
show(insects);
}// End of for loop
return 0;
}// End of main function

Sample Output:

Original data
Grasshopper Ant
Caterpillar Beetle
Mosquito Fly
Dragonfly Bee
Cockroach Spider

After modifying row: 1 last element.

Enter the new element: Dog
Grasshopper Dog
Caterpillar Beetle
Mosquito Fly
Dragonfly Bee
Cockroach Spider

After modifying row: 2 last element.

Enter the new element: Lion
Grasshopper Dog
Caterpillar Lion
Mosquito Fly
Dragonfly Bee
Cockroach Spider

After modifying row: 3 last element.

Enter the new element: Ox
Grasshopper Dog
Caterpillar Lion
Mosquito Ox
Dragonfly Bee
Cockroach Spider

After modifying row: 4 last element.

Enter the new element: Panda
Grasshopper Dog
Caterpillar Lion
Mosquito Ox
Dragonfly Panda
Cockroach Spider

After modifying row: 5 last element.

Enter the new element: Squirrel
Grasshopper Dog
Caterpillar Lion
Mosquito Ox
Dragonfly Panda
Cockroach Squirrel


Related Solutions

Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
Write a C program The Visual Studio project itself must make its output to the Console...
Write a C program The Visual Studio project itself must make its output to the Console (i.e. the Command Prompt using printf) and it must exhibit the following features as a minimum: 3%: Looping Menu with 3 main actions: View Cars, Sell Car, View Sales Note: A Car is defined by its price and model 3%: Must contain at least three arrays to record sales figures (maximum of 10 Car models) Two for recording the price and model of one...
c++   In this lab you will create a program to make a list of conference sessions...
c++   In this lab you will create a program to make a list of conference sessions you want to attend. (List can be of anything...) You can hard code 10 Sessions in the beginning of your main program. For example, I have session UK1("Descaling agile",    "Gojko Adzic",       60);        session UK2("Theory of constraints", "Pawel Kaminski", 90); and then:        List l;        l.add(&UK1);        l.add(&UK2); Your Session struct should have the following member data: The Title The Speaker The session...
Write a C program of car sale: The Visual Studio project itself must make its output...
Write a C program of car sale: The Visual Studio project itself must make its output to the Console (i.e. the Command Prompt using printf) and it must exhibit the following features as a minimum: 10%: Looping Menu with 2 main actions: Sell Car, View Sales Note: A Car is defined only by its price 10% Must contain at least one array containing sales figures (each entry represents the price of one vehicle) for a maximum of 10 Cars 5%:...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT