Resize vector countDown to have newSize elements. Populate the vector with integ
ID: 3766748 • Letter: R
Question
Resize vector countDown to have newSize elements. Populate the vector with integers {newSize, newSize - 1, ..., 1}. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs: 3 2 1 Go!
Code given:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> countDown(0);
int newSize = 0;
int i = 0;
newSize = 3;
/* Your solution goes here */
for (i = 0; i < newSize; ++i) {
cout << countDown.at(i) << " ";
}
cout << "Go!" << endl;
return 0;
}
Anything bolded cannot be edited. Please bold your inputed code to diffrentiate it from the rest.
Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> countDown(0);
int newSize = 0;
int i = 0;
newSize = 3;
/* Your solution goes here */
for(int i=newSize;i>=1;i--)
countDown.push_back(i);
for (i = 0; i < newSize; ++i) {
cout << countDown.at(i) << " ";
}
cout << "Go!" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.