DSA SheetLesson · no judge

DSA FUNDAMENTALSARRAY BASICS

Sum and Product Need Different Starting Values

Reading · 4 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 26

A sum and a product are two independent running values

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.

CPPOne traversal updates two separate accumulators.
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.

CHECKPOINT 1Not answered

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.

Sum must start at 0, but product must start at 1

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.

CPPThe declarations choose the correct identity value for each operation.
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.

CHECKPOINT 2Not answered

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 array [2, 3, 4] feeding a sum accumulator and a product accumulatorThe array has values 2, 3, and 4 at indices 0, 1, and 2. A left-to-right visit path crosses each index once. The sum track starts at 0 and changes to 2, 5, and 9. The product track starts at 1 and changes to 2, 6, and 24. After index 2, the final sum is 9 and the final product is 24.234025912624012visit ordersumproductsum = 9product = 24each visit updates both accumulators once
The same three visits build both answers.

Every visited element changes both running values

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.

TEXTThe two accumulators keep separate results after each visit.
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.

After the final update, the accumulators are the answers

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.

CPPPrinting after the loop reports the completed sum and product.
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.