Create two vectors. The first will store strings, the second will store doubles
ID: 3903195 • Letter: C
Question
Create two vectors. The first will store strings, the second will store doubles ? Create a loop that will run infinitely o Get user input for a string o If user input is “STOP”, then break out of the loop. o Store that string in the vector of strings ? Create a second loop that will run infinitely o Get user input for a double o If user input is “-999.00”, then break out of the loop. o Store that double in the vector of doubles ? Create a third loop that will run to the size of the smaller of the two vectors o For each iteration of the loop, print out the same index value of the string vector and the double vector ? i.e. if i = 0, print out stringvector[0] and doublevector[0];
Explanation / Answer
// Program is in c++
#include <bits/stdc++.h>
using namespace std;
int main()
{
std::vector<string> sVect; // Create an empty vector: string
string sIn; // variable to hold string input
vector<double> dVect; // Create an empty vector: double
double dIn = 0.0; // variable to hold double input
int sSize = 0, dSize = 0, loopSize=0; // variable to hold size of vectors and final loop
while(1) { // Infinite loop for string vector
std::cin >> sIn;
if(sIn == "STOP") // stop when user enters 'STOP'
break;
sVect.push_back(sIn); // add into vector
}
while(1) { // Infinite loop for double vector
cin >> dIn;
if(dIn == -999.00) // stop when user enters -999.00
break;
dVect.push_back(dIn); // add into vector
}
sSize = sVect.size(); // size of string vector
dSize = dVect.size(); // size of double vector
loopSize = dSize < sSize ? dSize : sSize;
for (int i=0; i<loopSize; i++) { // final loop
cout << sVect[i] << endl;
cout << dVect[i] << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.