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....
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...
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...
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...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT