int collapse(vector<int> result> needs correction!!

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?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int collapse(vector<int> s)
{
  vector <int > result;
    
    int sum = 0;
  if(s.size() % 2 ==0)
  {
      
      for(int i = 0; i < s.size() ; i +=2)
      {
          
          sum = s.at(i) + s.at(i +1);
            result.push_back(sum);
      }

  }
  if(s.size() % 2 == 1)
  {
      s.pop_back();
      for(int i = 0 ; i < s.size(); i+= 2)
      {
          sum = s.at(i) + s.at(i + 1);
          resut.push_back(sum);
      }
      
  }
    
    return 0;
}
Last edited on
johnnylo wrote:
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.

4th:
See line 23. You have a misspelling there.

Keep up the good work!

-Albatross
Last edited on
this should return a vector of ints right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
vector<int> collapse(vector<int> s)
{
  vector <int > result;
    
    int sum = 0;
  if(s.size() % 2 ==0)
  {
      
      for(int i = 0; i < s.size() ; i +=2)
      {
          
          sum = s.at(i) + s.at(i +1);
            result.push_back(sum);
      }

  }
  if(s.size() % 2 == 1)
  {
      result.pop_back();
      for(int i = 0 ; i < s.size(); i+= 2)
      {
          sum = s.at(i) + s.at(i + 1);
          result.push_back(sum);
      }
      
  }
    
    return result;
}
That looks about right to me.

-Albatross
thanks..
Challenge for you.

If I may paraphrase your code:

1
2
3
4
5
6
7
8
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.
Topic archived. No new replies allowed.