Question

In: Computer Science

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 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).

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What i have so far, but keeps on getting an error once the functions are called.

// StringsList.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include
#include
#include
#include
#include

using namespace std;

void funcWithList(list &, const std::string &);
void replacementForList(list &, const std::string &);

int main()
{
   ofstream out_data("Output.txt", ios::out);
   //end of copy code
   if (!out_data)
   {
       cout << "Error in opening the file, Output.txt";
       getchar();
       exit(1);

   }

std::cout << "Hello World!\n";

   list aList; //create list taking std::string
std::string input;

//get input to change last string

std::cin >> input;
   out_data << input;
   std::string s = input;

   //I can pass list to the function like that:
   funcWithList(aList, s);
   replacementForList(aList, s);

   getchar();
   out_data.close();//close output.txt file
   return 0;
}

void funcWithList(list &, const std::string &)
{
   ofstream out_data("Output.txt", ios::out);
   //end of copy code
   if (!out_data)
   {
       cout << "Error in opening the file, Output.txt";
       getchar();
       exit(1);

   }

   list tempList //myList.push_front;


   //I can access the last element like that:
   std::cout << "\nThe last element is: " << & tempList.back();

tempList.pop_back();  

std::string input;

//get input to change last string

std::cin >> input;
   out_data << input;
   std::string s = input;


   tempList.push_back(s);
}

void replacementForList(list &, const std::string &)
{
   ofstream out_data("Output.txt", ios::out);
   //end of copy code
   if (!out_data)
   {
       cout << "Error in opening the file, Output.txt";
       getchar();
       exit(1);

   }

   list tempList //myList.push_front;


   //I can access the last element like that:
   std::cout << "\nThe last element is: " << &tempList.back();

tempList.pop_back();  

std::string input;

//get input to change last string

std::cin >> input;
   out_data << input;
   std::string s = input;


   tempList.push_back(s);
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

no examples or expected outputs were given but did state: "The main creates the list, creates the string variables, populates few elements in the list (a 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."

therefore would look something like...

Fill your list with names...

Freddie

Nick

James

Rick

Now function calls

modify using references

what new name to use?

Robert

print from main, the last element

Robert

modify using deletion

what new name to use?

Tim

print from main, the last element

Tim

Solutions

Expert Solution

Code:

#include <list>
#include <string>
#include <iostream>

using namespace std;

void funcWithList(list<string> &, const std::string &);
void replacementForList(list<string> &, const std::string &);

int main()
{

   list<string> aList; //create list taking std::string

   aList.push_back("Freddie");
   aList.push_back("Nick");
   aList.push_back("James");
   aList.push_back("Rick");

   cout << "\nNames in the list\n";

   list<string>::iterator it = aList.begin();

   while (it != aList.end())
   {
       cout << "\n" << *it;
       it++;
   }

   cout << "\nNow function calls\n";
  
   std::string input;

   //get input to change last string

   cout << "\nModify using reference\n";
   cout << "\nWhat name to use?\n";

   std::cin >> input;
   std::string s = input;

   //I can pass list to the function like that:
   funcWithList(aList, s);

   cout << "\nPrint from main, Last Element :" << aList.back();

   cout << "\nModify using Deletion\n";
   cout << "\nWhat new name to use?\n";

   std::cin >> input;
   std::string s1 = input;

   replacementForList(aList, s1);

   cout << "\nPrint from main, Last Element :" << aList.back();


   getchar();
   return 0;
}

void funcWithList(list<string>& aList, const std::string & s)
{
   //In place Replacement
   aList.assign(aList.size() - 1, s);
}

void replacementForList(list<string>& a_list, const std::string & s)
{
   //Remove the last element and replace
   a_list.pop_back();
   a_list.push_back(s);
}

OUTPUT:


Related Solutions

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....
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...
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...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words)...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter...
MUST BE DONE IN C (NOT C++) This program should utilize the basics of strings. First,...
MUST BE DONE IN C (NOT C++) This program should utilize the basics of strings. First, declare and initialize a string. You can name the variable whichever way you want and you can initialize it to whichever value you want. Then, use a for loop to print its characters; one at a time, until you reach the null character. After this, go ahead and declare a second string (since you are not initializing it right away, you will have to...
IN C LANGUAGE This program will read in a series of strings and print only the...
IN C LANGUAGE This program will read in a series of strings and print only the consonants (including Y) until the word "stop" appears. No string will be longer than 100 characters. A consonant is any letter that is not a vowel. Don't forget to follow the standard read pattern! Examples Enter a string: Hello Hll Enter a string: World! Wrld Enter a string: 123! Enter a string: stop Enter a string: stop
Write a C++ program to help the Administration of a football league to manipulate the list...
Write a C++ program to help the Administration of a football league to manipulate the list of players registered in different teams. There are 26 teams participating in the league, each is denoted by a letter in the range A to Z. Each team can have 11 players at most. The information of all teams' players are stored in a text file named 'players.dat'. Each line from the input file contains the details of one player; where the player's details...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT