So what happens when you want to change each of the values within a vector?
You can use a for
loop!
For example, suppose we have an int
vector that looks like this:
You can write a for
loop that iterates from 0
to vector.size()
. And here’s the cool part: you can use the counter of the for
loop as the index! Woah.
for (int i = 0; i < vector.size(); i++) { vector[i] = vector[i] + 10; }
This will change the vector to:
Here, we incremented i
from 0
to vector.size()
, which is 3. During each iteration, we are adding 10 to the element at position i
:
- When
i = 0
, we added 10 tovector[0]
- When
i = 1
, we added 10 tovector[1]
- When
i = 2
, we added 10 tovector[2]
Instructions
In the code editor, there is a double
vector named delivery_order
that is storing the prices of some food items.
There is also a double
variable named total
that’s already declared and initialized.
Write a for
loop that iterates through the delivery_order
vector (from 0
to delivery_order.size()
). And add each order to total
during each iteration.
Output the final total
using std::cout
.