create a function named collapse. the function takes a vector as an only parameter. the result should return a vector.
for example: a vector v has the following elements : {2, 3, 4, 5, 6, 7, 7,8}
compute the sum of the first 2 elements inside vector v and store it in a vector. ( if 2 + 3 = 5, 4 + 5 = 9, 6 + 7 = 13, 7 + 8 = 15) it should return a vector with the sum of every 2 elements inside the vetor .like this {5, 9, 13, 15}.
if the vector has an odd length. then the last element will be left out.
i'm newbie to c++ so i really need help.
here are the things that i'm not sure about:
did i declare the function correcly?
will the results return a vector?
did i set the condition for the loop correctly?
will the results store in a vector?
did i declare the function correcly?
will the results return a vector?
did i set the condition for the loop correctly?
will the results store in a vector?
No.
No.
Yes.
Not always.
Detailed breakdown of 2-4:
2nd:
You declared your function to return an int, not a vector of ints, and you didn't pass any arguments as references (which could be a bad idea considering what you were asked).
Major hint: vector<int> collapse(vector<int> s)
3rd:
Those look correct, though I might be missing something inherently obvious.
vector collapse( vector s )
{
if s has an even number of elements then
do_stuff;
if s has an odd number of elements then
remove last element;
do_stuff;
}
Rewrite your function so that you only write
"do_stuff;" once.