Four programs that report a number about some data. All four run. All four print a number. Every one of those numbers is wrong.
There is nothing to fix in the data here — the datasets are fine. The faults are all in the arithmetic, one of them breaks a C# rule you have met before, and none of them announces itself.
The technique for this page is to check every number against something you can work out yourself. All four are small enough to do on paper, which is exactly why they were chosen.
This should report the compression ratio. It reports 3.33.
int[] original = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 };int[] encoded = { 0, 9, 1, 9, 0, 2 };
double originalSize = original.Length;double encodedSize = encoded.Length;double ratio = originalSize / encodedSize;
Console.WriteLine($"Original: {original.Length} numbers");Console.WriteLine($"Encoded: {encoded.Length} numbers");Console.WriteLine($"Compression ratio: {ratio:F2}");Also: a colleague reads 3.33 and says "great, three times smaller." They have reached the right conclusion from a wrong number. Explain how that is possible, and why it makes this bug more dangerous rather than less.
This should report how many colors a given bit depth can make. It says 8 bits per channel gives 4096.
Console.WriteLine($"2 bits per channel: {ColorCount(2)} colors");Console.WriteLine($"8 bits per channel: {ColorCount(8)} colors");
int ColorCount(int bitsPerChannel){ int levels = bitsPerChannel * 2;
return levels * levels * levels;}Also: the real answer for 8 bits is a number you have seen before. State it, and say where in the course you met it.
Three judges scored a skateboard run 7, 8 and 8. This reports an average of 7.00.
average is a double, and it still lost the decimal. Look at what is being divided before the answer is stored.
int[] scores = { 7, 8, 8 };
int total = 0;
for (int i = 0; i < scores.Length; i++){ total = total + scores[i];}
double average = total / scores.Length;
Console.WriteLine($"Judges' scores: {scores[0]}, {scores[1]}, {scores[2]}");Console.WriteLine($"Average score: {average:F2}");Also: work out the correct average by hand. Then say how far off the program was, and whether you would have noticed if you had not been told to check.
This filters out an impossible age and then reports the average of what is left. It says the average of four teenagers is 266.2.
int[] ages = { 17, 16, 999, 18, 15 };
int kept = 0;
for (int i = 0; i < ages.Length; i++){ if (ages[i] >= 13 && ages[i] <= 19) { kept = kept + 1; }}
double total = 0;
for (int i = 0; i < ages.Length; i++){ total = total + ages[i];}
Console.WriteLine($"Kept {kept} valid ages out of {ages.Length}");Console.WriteLine($"Average of the valid ages: {total / kept:F1}");Also: the filtering loop is correct and the counting is correct. Name the exact line where the program stops talking about the data it said it was talking about.
For each program: one sentence on what was wrong, the corrected line, and the answers to the Also questions.
And one more, for the set as a whole: Bug 4 printed 266.2 as the average age of four teenagers. Say what check, applied to any of these four programs, would have caught the fault immediately — and why nobody applies it.
It is collected as Homework 10, together with Question Set 13: Reading Passage. The drop is worth 5 points for everything in it, graded on completion.