What the code is supposed to do
PrintPositiveAndNegative(int[] nums) goes through an array and prints one line per number. If the number is less than zero, the line is the word negative. Otherwise the line is the number itself, which includes zero. For {2, -12, 0, 4, -5, 3} the expected output is:
2
negative
0
4
negative
3
The starter code prints nine lines instead of six: a stray positive before each positive number, and the negative numbers printed as plain numbers.
public void PrintPositiveAndNegative(int[] nums) {
foreach (int num in nums) {
if (num > 0) {
Console.WriteLine("positive");
}
Console.WriteLine(num);
}
}
What goes wrong
There are two separate problems, and both show in the wrong output.
- The condition picks the wrong numbers.
num > 0is true for 2, 4 and 3, so those get an extra line that should not exist. For -12 and -5 the condition is false, so they never get the wordnegative. The message belongs to the numbers below zero, so the condition should benum < 0, and the text should be"negative". - The last line runs every time.
Console.WriteLine(num)sits after theif, not inside a branch, so it runs for every element. Even after the first problem is fixed, a negative number would printnegativeand then its value. Each number must produce exactly one line, so the two prints need to be alternatives.
Fixing it step by step
- Change the condition to
num < 0, so it selects the numbers that need a special message. - Print
"negative"inside that branch instead of"positive". - Add an
elsebranch and moveConsole.WriteLine(num)into it. Theelseruns exactly when the condition is false, that is, when the number is zero or greater.
The fixed code in C#
public void PrintPositiveAndNegative(int[] nums) {
foreach (int num in nums) {
if (num < 0) {
Console.WriteLine("negative");
} else {
Console.WriteLine(num);
}
}
}
public void MainFunction() {
PrintPositiveAndNegative(new int[]{2, -12, 0, 4, -5, 3});
}
Tracing it: 2 is not below zero, so the else prints 2. -12 is below zero, so negative is printed and the else is skipped. 0 is not below zero, so the else prints 0. The remaining numbers follow the same two paths, which gives the six expected lines.
Some details worth knowing:
foreachvisits each element of the array in order and gives it to you asnum. You can readnumbut not assign to it inside the loop.- Zero goes to the
elsebranch. Because the test is< 0, not<= 0, zero prints as0, as the task asks. Console.WriteLineaccepts both a string and anint. The two branches print different types, which is fine because each overload adds the line break itself.- The array literal
new int[]{...}creates the array. Arrays in C# have a fixed length, which is why the lesson usesint[]here.
Common mistakes
- Using
<= 0. Zero would printnegative, but zero is not less than zero. - Printing the number after the
if/else. That prints one extra line for every negative number. - Two separate
ifstatements (if (num < 0)andif (num >= 0)). This gives the right output but repeats the test;elsesays directly that the second case is the opposite of the first. - Wrong capitalisation of the word. The output must read exactly
negative, in lowercase.
Checking your fix
Run the code and compare with the six expected lines. If you still see the word positive, the condition or the text is unchanged. If a negative number appears as a number, the condition is still testing the wrong sign. If you get more than six lines, one print is outside the else.