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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.