FAQ: Vectors - Review

This community-built FAQ covers the “Review” exercise from the lesson “Vectors”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why do you declare the int product_odd as being 1 at the beginning of the code? I thought generally you set it to 0…?? Is it something to do with the fact you will be multiplying it??

2 Likes

multiplication can’t be done by 0.
03 would be 0. Then again 01 would be 0 and then 0*9 =0.

If it was set to 2, the total would be 54

2 Likes

Is anyone having a problem with this exercise in which, you complete the task but it is forever attempting to verify if I did the problem correctly? I’ve been waiting about 5 minutes now and normally it only takes a matter of seconds

jokes i refreshed the page and now it works

Why do you declare the int total_even = 0 ?

1 Like
#include <iostream>

int main() {
	std::vector<int>numbers = {2, 4, 3, 6, 1, 9};
	int even_total = 0;
	int odd_total = 0;
  for (int i = 0; i < numbers.size(); i++)
  {
    if (even_total % 2 == 0)
    {
     even_total = even_total + numbers[i]; 
    }
    if (odd_total % 2 == 1)
    {
      odd_total = odd_total + numbers[i];
    }
  }
 std::cout << "Sum of even numbers is " << even_total;
 std::cout << "Sum of odd numbers is " << odd_total;
}

Is my code but it errors

review.cpp: In function 'int main()':
review.cpp:4:7: error: 'vector' is not a member of 'std'
  std::vector<int>numbers = {2, 4, 3, 6, 1, 9};
       ^~~~~~
review.cpp:4:14: error: expected primary-expression before 'int'
  std::vector<int>numbers = {2, 4, 3, 6, 1, 9};
              ^~~
review.cpp:7:23: error: 'numbers' was not declared in this scope
   for (int i = 0; i < numbers.size(); i++)
                       ^~~~~~~

It seems to say that std::vector isn’t an expression, even though I was taught that it is.

Hey Speed_reader,

You just forgot to add the library for vector in your code. Below the #include iostream you need #include vector for your program to recognize std::vector. Hopefully you figured this out.

Cheers!

I don’t understand how “if (vector[i] % 2 == 0)” is able to find the even numbers in the vector.

To my understanding, this line is saying “okay, use that value you just pulled from the vector and divide it by two. If you get zero, then yep, that’s an even number, now do the following.” But none of the values in the vector would yield zero if you cut them in half.

Does that make sense? What am I missing here?

% doesn’t mean divide. It’s the modulo operator. It returns the remainder of dividing the dividend by the divisor. If any element in the vector divided by 2 has a remainder of 0 then it’s an even number. Hope this helps!

2 Likes

Ahhhh! Yes, that makes perfect sense now, thank you so, so much. That’s been bugging me all day.

1 Like

For some reason if I #include it just loads forever when I press run. If i leave that part out then it errors out which makes sense but then I can’t check if my program actually works.

Hello, @microblaster90973.

Welcome to the forum.

Sounds like you may have an infinite loop in your code. If you’ll include your code in a post using these guidelines, we can take a look.

Why do you declare the int total_even = 0 ?

If I wanted to print out under each for and else loop the value on the vector that was chosen, how would I do that so I can see what is happening behind the scenes. Thank you.

#include
#include

int main() {

int total_even = 0;
int product_odd = 1;

std::vector vector = {2, 4, 3, 6, 1, 9};

for (int i = 0; i < vector.size(); i++) {

if (vector[i] % 2 == 0) {

  total_even = total_even + vector[i];

} else {

  product_odd = product_odd * vector[i];

}

}

std::cout << "Sum of even: " << total_even << “\n”;
std::cout << "Product of odd: " << product_odd;

}