Answer to Question #2771 in C++ for will

Question #2771
I cant seem to get this programm to run and i dunno what the output will be can some one help me ?
the code is
vector <int> myinventory(4);
myinventory[0] = 2;
for(int i = 1; i <= 3; i++)
& {
& & myinventory[i] = 2*myinventory[i - 1];
& & }
myinventory.push_back(34);
for (int i = 0; i< myinventory.size();i++)
& & {
& & & & cout << myinventory[i] <<" ";=""
& & }
1
Expert's answer
2011-05-16T11:21:33-0400
1) First you should define variable as follows:

& vector<int> myinventory(4);
Instead of
vector myinventory(4);

2) the last line
cout<<myinventory[i] <<" ";=""
& contains an error. It should be like this:
cout<<myinventory[i] <<"; ";

Corrected program:

vector<int> myinventory(4);
myinventory[0] = 2;
for(int i = 1; i <= 3; i++)
{
& myinventory[i] = 2*myinventory[i - 1];
}
myinventory.push_back(34);
for (unsigned int i = 0; i<myinventory.size();i++)
{
cout<<myinventory[i] <<"; ";
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS