DSA FUNDAMENTALS › ARRAY BASICS
For the array [2, 3, 4], one left-to-right traversal can calculate both answers. Keep one variable for the sum and another for the product. Each variable represents the result of its own operation over the elements visited so far, so changing one does not replace or modify the other.
int sum = 0;
int product = 1;
for (int i = 0; i < 3; i++) {
sum += arr[i];
product *= arr[i];
}After visiting 2, the sum records 2 and the product records 2. After visiting 3, they record 5 and 6. After visiting 4, they record 9 and 24. The loop follows the same path for both calculations, but each accumulator applies a different operation to the current element.
Which initial pair should you use before processing [2, 3, 4], written as (sum, product)?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The initial value must leave the operation's first element unchanged. Adding 0 does that, so sum starts at 0. Multiplying by 1 does that, so product starts at 1. These are not arbitrary defaults: they are the values that let the first visited element become the first real result.
int sum = 0;
int product = 1;
for (int i = 0; i < 3; i++) {
sum = sum + arr[i];
product = product * arr[i];
}If product starts at 0, the first update calculates 0 * 2, which is 0. The next updates calculate 0 * 3 and 0 * 4, so the final product is incorrectly 0 instead of 24. The sum does not have this problem because adding 0 does not erase the value that follows.
Complete the two loop-body statements so the current value arr[i] updates both accumulators.
for (int i = 0; i < 3; i++) {
// update sum and product using arr[i]
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The loop body must contain both updates because every element belongs to both calculations. For [2, 3, 4], the states after each visit are easy to trace as pairs of (sum, product): (2, 2) after index 0, (5, 6) after index 1, and (9, 24) after index 2. The sum adds the current value, while the product multiplies by that same value.
Start: (0, 1)
After 2: (2, 2)
After 3: (5, 6)
After 4: (9, 24)The two updates can share one loop because they use the same current element and the same index. They still represent different calculations. Removing either update gives you only one answer, even though the traversal itself still visits all three elements.
The final answers exist only after index 2 has been processed. At that moment, sum is 9 and product is 24. Code that prints after the loop reports those completed results, because no element remains to change either accumulator.
for (int i = 0; i < 3; i++) {
sum += arr[i];
product *= arr[i];
}
cout << "Sum = " << sum << '\n';
cout << "Product = " << product << '\n';If you print inside the loop instead, the output includes intermediate states after 2 and after 3 before it reaches the final state after 4. Those states are useful while tracing the loop, but output after the loop gives only the requested final sum and product.