What the code is supposed to do
ComputeSum(int n) returns the sum of the whole numbers from 0 up to n - 1. For ComputeSum(4) that is 0 + 1 + 2 + 3 = 6, and MainFunction prints the result. The starter code prints 16 instead.
public int ComputeSum(int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += n;
}
return sum;
}
What goes wrong
The number 16 is the clue: it equals 4 + 4 + 4 + 4. The loop runs four times, and on each pass it adds n, which is the method's argument and stays 4 the whole time. The total is therefore n * n, not the sum of the numbers below n.
The loop counter i is the value that changes from one pass to the next: it is 0, then 1, then 2, then 3. The sum is supposed to collect those values, but the body never mentions i, so the counter does its job of repeating the body and contributes nothing to the total.
Fixing it step by step
- Notice that the loop already visits the right numbers. With
i = 0andi < n, the counter takes the values 0 ton - 1, which is exactly the range the task wants. - Change what is added. Replace
sum += nwithsum += i. - Leave the rest alone. The initial value, the loop header and the return are correct.
The fixed code in C#
public int ComputeSum(int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += i;
}
return sum;
}
public void MainFunction() {
Console.WriteLine(ComputeSum(4));
}
Tracing ComputeSum(4), the value of sum after each pass is:
i = 0:sumis 0.i = 1:sumis 1.i = 2:sumis 3.i = 3:sumis 6.
The loop ends because i becomes 4, which is not less than n. The method returns 6, and the program prints:
6
Notes on the C# code:
- The upper bound is exclusive. With
i < n, the valuenitself is never added. The lesson asks for the sum up ton - 1, so this matches. - The method returns, and
MainFunctionprints. The return type isint, so the caller gets the value and decides what to do with it.Console.WriteLineaccepts anintdirectly. - PascalCase names. The method is
ComputeSum. A lowercasecomputeSumis a different, undeclared name in C#. - Overflow. An
intthat gets too large wraps to a negative value without an error. For the sum of 0 ton - 1, this starts to happen oncenreaches 65,537. The lesson's tests use small values.
How to find this kind of bug
When the output is wrong, compare it with the expected value and look for a pattern. Here 16 is 4 * 4, which points at "n added n times". A second technique is to write out the first pass or two by hand with real values. On the first pass the buggy code does sum += 4, while the correct one should do sum += 0. The mismatch shows up at once.
Common mistakes
- Changing the loop instead of the body. Starting at 1 or using
<=changes which numbers are visited. With<=andsum += i, the method returns 10 for 4, not 6. - Replacing
suminstead of adding to it. Writingsum = ikeeps only the last number, 3. - Using the formula blindly. The closed form for 0 + ... + (n - 1) is
n * (n - 1) / 2. It is correct, but the lesson practises the loop, so keep the accumulator.
Checking your fix
Run the code. The output should be 6. Also check by hand that ComputeSum(0) and ComputeSum(1) both return 0: the first never enters the loop, and the second adds only 0.