In: Computer Science
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.
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