FAQ: Vectors - Operations

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

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

Learn C++

FAQs on the exercise Operations

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!

#include
#include

int main() {

std::vector delivery_order;

delivery_order.push_back(8.99);
delivery_order.push_back(3.75);
delivery_order.push_back(0.99);
delivery_order.push_back(5.99);

// Calculate the total using a for loop:

double total = 0.0;

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

delivery_order[i] == 3.75 + 0.99 + 5.99 + 8.99;

}

total == delivery_order[0];
std::cout << total << “\n”;

}

Why this program gives ‘0’ as the output? I wrote total == delivery_order[0]. In the for loop I declared delivery_order[i] == 3.75 + 0.99 + 5.99 + 8.99. So I feel that total == sum. Where am I wrong?

I understood the first point (i.e I should not use ‘==’ sign). But I did not understand the second point which says that I should have written total = delivery_order[i] and not total = delivery_order[0]. I think that both of these will give me the correct result as each element of the vector is equal to the same value.

First, after these lines of code execute:

std::vector <double> delivery_order;

delivery_order.push_back(8.99);
delivery_order.push_back(3.75);
delivery_order.push_back(0.99);
delivery_order.push_back(5.99);

you have a vector that looks like this: [8.99, 3.75, 0.99, 5.99] These elements of the vector can be accessed by their index (position) like so: delivery_order[0] The value of the element at index 0 is 8.99. The value at index 1 is 3.75, index 2 is 0.99 and index 3 is 5.99
Your for loop is supposed to be used to calculate the sum of those 4 values. The reason you are getting 0 as the output is this line: total == delivery_order[0] Since you used ==, you are testing a condtion. That line of code is testing whether the value of total which is still 0.0 is equal to the value of delivery_order[0] which is 8.99. That evaluates to false which is stored as 0 in C++.

To get the total as instructed, you need to access each element of the vector, one at a time, and add its value to the total variable. That is the purpose of the for loop.

Try to re-write your for loop’s code body, and get the correct result. I’ve included the solution below if you get stuck. Happy coding!

for (int i = 0; i < delivery_order.size(); i++) {
  total += delivery_order[i]; //this will add  each value in turn to the total
}

std::cout << total << "\n";
1 Like

My second post after 14 days was meant for Mohamed Abdelkarem who answered my question. I received it in my email account. Somehow his post is not visible here. Do you have any idea why is it so?
I should not have used the ‘==’ sign in my program in the question. You wrote that " To get the total as instructed, you need to access each element of the vector, one at a time, and add its value to the total variable. That is the purpose of the for loop." What is the problem if I make each element of the vector equal to the sum of four numbers and equate the variable ‘total’ to any single element of the vector(all the elements are equal to sum of numbers in the vector)

I assume you’re referring to this line:

delivery_order[i] == 3.75 + 0.99 + 5.99 + 8.99;

What if you didn’t know what the 4 values were? What if you needed the 4 original values of the vector for a later operation? Your line of code above changes all of the values of the original vector to be the sum of the original values. Why would you need the same sum stored 4 times in a vector? Learning to access the values stored in an indexed collection of data without prior knowledge of those values, and without mutating them is an important concept in every programming language.

1 Like

I know that ‘total += delivery_order[i]’ essentially means ‘total = total + delivery_order[i]’. Can I use ‘total = total + delivery_order[i]’ in my function? (i.e is it correct syntax)
Thanks for all the answers.

You can, and it’s perfectly fine syntactically, it’s just shorter to use +=. Opting to use total = total + delivery_order[i] is really just a matter of personal preference. There’s nothing wrong with choosing readability and clarity over being concise.

1 Like