FAQ: Vectors - Review

Great info, thank you!

Ok I’m having trouble understanding the significance of a line in the for loop.

for (int i = 0; i < vector.size(); i++) {
if (vector[i] % 2 == 0) {
toteven = toteven + vector[i]

particularly what is the purpose of the [i] in vector [i]
is it because it is an intiger, if thats the case and we had doubles would it be [d]?

The variable i is used to keep track of the index.

It does not matter which letter you use for that, as long as you’re consistent … change every i variable to be j for example, and it will still do the same thing.

The idea is that you’re accessing stuff at different positions in vector.
For example, vector[3] would get stuff in vector at index 3.

This is done in a for loop here,
so i starts as being 0 … and you have vector[0] on that iteration
next, i is 1 … and you have vector[1] on that iteration
next, i is 2 … and you have vector[2] on that iteration
and so on …
but it stops when i reaches the size of the vector (meaning the last iteration is when i is one less than the size).

2 Likes

Thank you so much!!! I was racking my head around the concept for so long.

1 Like

Just finished my code and want to share with people.
#include

#include

using namespace std;

int main(){

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

double i, sumE;

int sumO;

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

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

  sumE = sumE + array[i];

} else {

  sumO = sumO * array[i];

}

}

cout << "Sum of even numbers is " << sumE <<endl;

cout << "Sum of odd numbers is " << sumO <<endl;

return 0;

}

This is my code and I cannot for the life of me understand why it doesn’t work. It gives the sum 2 and the product 1, and to me it looks virtually identical to the solution. Maybe you guys can let me know. I also noticed that in my code I get a compilation error if I don’t declare i before the for. In the solution, i is not declared before the for and it still works.

#include <iostream> #include <vector> int main() { std::vector<int> numbers = {2, 4, 3, 6, 1, 9}; int sum = 0; int product = 1; int i; for (int i = 0; i < numbers.size(); i++); { if (numbers[i] % 2 == 0) { sum = sum + numbers[i]; } else { product = product * numbers[i]; } } std::cout << "Sum of even numbers is " << sum << ". \n"; std::cout << "Product of odd numbers is " << product << ". \n"; }

That’s a pretty good clue. If you read the error message, it’s an even better clue.

script.cpp: In function ‘int main()’:
script.cpp:14:15: error: ‘i’ was not declared in this scope
   if (numbers[i] % 2 == 0) {
               ^

The error is telling you that i isn’t defined (has not been declared) inside the loop. How can that be when you clearly declared it here:

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

Examine the entire line where your for loop starts. See anything amiss?

Hint

Take a close look at your for loop:

for (int i = 0; i < numbers.size(); i++); {
// what is this doing here?             ^
  if (numbers[i] % 2 == 0) {

    sum = sum + numbers[i];

  } else {

    product = product * numbers[i];

  }
}
2 Likes

For some reason my product calculates as 0. However, if I change product for a sum works perfect. Dont understand what is the problem

#include <iostream>
#include <vector>
using namespace std;

int main () {
  vector<int> v1={2,4,3,6,1,9};

  int total_even = 0;
  int total_odd =0;

  for (int i=0; i < v1.size(); i++) {
    if(v1[i]%2 == 0)
    total_even = total_even + v1[i];
    else{
    total_odd = total_odd * v1[i];}
  }

  cout << "Sum of even numbers is " << total_even << endl;
  cout << "Product of odd numbers is " << total_odd << endl;
  return 0;
}

Figured it out; had to start with 1 for the product

Hi,
This is my code:
#include
#include

int main(){
std::vector number = {2,4,3,6,1,9};
int even;
int odd;

for(int i = 0; i < number.size(); i++) {
if(number[i] % 2 == 1){
odd += number[i];
} else {
even += number[i];
}
}

std::cout << "Sum of even numbers is " << even << std::endl;
std::cout << "Product of odd numbers is " << odd;

return 0;
}

This is the output:
Sum of even numbers is 32776
Product of odd numbers is 4197611

It works if I initialize even and odd as 0, but I’m confused as to why the output was a very big number when I didn’t initialize it

I just don’t understand this part of the solution:

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;

}

I don’t understand where if (vector[i] % 2 == 0) { comes from.
Someone please explain it this to me. Help.

We want to decide whether a number is even or odd. The % operator helps us do this. This operator divides and gives the remainder (Lesson: Arithmetic Operators).
If a number is even, then it will be perfectly divisible by 2. In other words, the remainder will be 0.
If a number is odd, then it will not be perfectly divisible by 2. In other words, the remainder will be 1.
Dividing an integer by 2 will result in a remainder of either 0 or 1. The integer will either be even or odd. There is no other possibility.

Suppose we have a vector {2, 4, 3, 6, 1, 9}
The for loop iterates over this vector.
In the first iteration, i is 0 and vector[0] will be 2.

The condition vector[i] % 2 == 0 checks whether the current number being iterated upon is even.

In the first iteration 2 % 2 will be 0 and therefore 2 is an even number.
In the second iteration 4 % 2 will be 0 and therefore 4 is an even number.
In the third iteration 3 % 2 will be 1 and therefore 3 is an odd number.
And so on.

This helps quite a bit, thank you so much!

1 Like

#include
#include

int main() {
std::vector numbers = {2, 4, 3, 6, 1, 9};

int even = 0;
int odds = 1;

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

if (numbers[i] % 2 == 0){
  even = even + numbers [i];
}

else{
  odds = odds * numbers [i];
}

std::cout << "Sum of even numbers is " << even << “\n”;

std::cout << "Product of odd numbers is " << odds << “\n”;

When I run it the output is like below:
Sum of even numbers is 2
Product of odd numbers is 1
Sum of even numbers is 6
Product of odd numbers is 1
Sum of even numbers is 6
Product of odd numbers is 3
Sum of even numbers is 12
Product of odd numbers is 3
Sum of even numbers is 12
Product of odd numbers is 3
Sum of even numbers is 12
Product of odd numbers is 27

}

}

nvm I just realized that it’s bcs of the scope within the for statement

Here’s my code, pretty easy problem ngl

#include <iostream>
#include <vector>

bool isEven(int num) {
    return num % 2 == 0;
}

int main() {

    std::vector<int> numbers = {2, 4, 3, 6, 1, 9};

    int sum = 0;
    int product = 1;

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

        if (isEven(numbers[i])) {

            sum += numbers[i];

        } else {

            product *= numbers[i];

        }
        
    }


}

I am confused as to why we are adding the vector to the total even. if it just asks for the sum of the even numbers, I would think it would just be “total_even” by itself. Thank you for any help.
Edit: I think I figured that kart out. It is progressively adding the numbers in each vector before it to create the total. I am left with s new question though. How does it know not to include the odd number? If it’s not even dies it just mark it as zero? And vise versa for the odd?

Does anyone know why I can not cout the vector numbers? the error " I am getting is error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘std::vector’)
cout << numbers << endl;

any help would be greatly appreciated. 

#include <iostream>
#include <vector>

using namespace std;

int main() {

vector<int> numbers = {2, 3, 4, 5, 6};
int sum = 0;
int product = 1;
//
for(int i = 0; i < numbers.size(); i++){
   
 if(numbers[i] % 2 == 0) {
   sum = numbers[i] + sum;
 }
 else {
   product = product * numbers[i];
 }
}

cout << "Sum of even numbers is " << sum << "\n";
cout << "Product of the odd numbers is " << product << "\n";
cout << numbers << endl;
}
1 Like
cout << numbers << endl;

You can’t print the whole vector using cout << (unless you overload the operator).

You could use a range-based loop e.g.

for (const auto& num: numbers) {
    cout << num << endl;
}

or iterate over the indices

for (int i = 0; i < numbers.size(); i++) {
    cout << numbers[i] << endl;
}

Here are a few resources to explore (or you could google to explore the topic in more depth):

2 Likes