Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Exercises please complete the following c++ exercises a) Write a code snippe

ID: 3821717 • Letter: C

Question

C++ Exercises

please complete the following c++ exercises

a) Write a code snippet which asks the user to enter city names until "DONE" is entered and stores the cities entered in a string vector. Then print the cities that were entered but in reverse order.

b)Write a code snippet that asks the user to enter how many scores will be input and allocates an array with the requested capacity. Then ask the user again and allocate a new array using the same variable used for the previous array. What should you do to avoid leaking the memory of the previous array?

Explanation / Answer

Answer:

a)

#include <iostream>
#include <vector>
using namespace std;

int main()
{
   //code
   vector<string> cities;
   int i,length;
   string word;
   cout<< "vector size = " << endl;
   cin>>length;
  
   while (getline(cin, word))
{
if (word == "done")
{
break;
}
cities.push_back(word);
}
  
for (decltype(cities.size()) i = 0; i <= length - 1; i++)
{
cities.push_back(word);
//cities=cities+word;
}
  
for (decltype(cities.size()) i = 0; i <= length - 1; i++)
{
cout<<word<<endl;
}
  
   return 0;
}