This is a C++ question. 1. Define a global vector<int> gv; initialize it with te
ID: 3734523 • Letter: T
Question
This is a C++ question.
1. Define a global vector<int> gv; initialize it with ten ints 1,2,4,8,16 etc.
2. Define a function f() takjing a vector<int> arguement
3. In f():
a) Define a local vector<int> lv with the same number of elements as the arguent vector
b) Copy the values from gv into lv
c) Print out the elements of lv
d) Define a local vector<int> lv2; initialize it to be a copy of the argument vector
e) Print out the elements of lv2
4. in main()
a) call f() with gv as its argument
b) Define a vector<int> vv and initialize it with the first ten factorial values (1, 2*1, 3*2*1, etc)
c) call f() with vv as argument
I dont understand how to solve this
Explanation / Answer
Please find the code below with detailed inline comments.
CODE
======================
#include <iostream>
#include <vector>
using namespace std;
void f(vector<int> gv) {
vector<int> lv;
vector<int> lv2;
for(int i=0; i!=gv.size(); i++){
lv.push_back(gv[i]);
}
for(int i=0; i!=lv.size(); i++){
cout << lv[i] << endl;
}
cout << endl;
lv2 = gv;
for(int i=0; i!=lv2.size(); i++){
cout << lv2[i] << endl;
}
cout << endl;
}
int main() {
vector<int> gv{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
f(gv);
vector<int> vv{1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
f(vv);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.