Back to Challenge Set 1

Debug Challenge: Arithmetic

Difficulty  

divider

Objective

Four short programs. Every one of them is broken. Find the mistake, say what kind of mistake it is, and fix it.

You did not write these, which is the point. Reading someone else's broken code is a different skill from writing your own, and it is the one you will use most — including on the AP exam, where you will be asked to describe a change that would break a program.

Skills to Practice

  • Telling a compiler error from a runtime error from a logic error
  • Order of operations
  • Choosing between % and /
  • Forcing real division instead of truncating

Three Kinds of Mistakes

Not every bug looks the same, and the difference changes how you hunt for it.

  • A compiler error means C# cannot build your program at all. Nothing runs — not even the lines before the mistake.
  • A runtime error means the program built fine and started running, then hit something it could not recover from partway through. Whatever printed before that point stays printed.
  • A logic error means the program built, ran start to finish, and did exactly what you told it — but what you told it was wrong. It finishes, it prints something, and the something is incorrect. There is no error message at all.

Logic errors are the dangerous ones, because nothing warns you. The only way to catch them is to know what the answer should be and notice that it isn't.

A Warning About Compiler Errors

Some languages report a syntax mistake several lines past where it actually is. C# is usually better than that — the compiler tends to name the exact line and column where something stopped making sense. Trust the number first; only widen your search if the named line looks fine on its own.

One thing C# does not do: run any part of a program that fails to build. A misspelled variable name is caught before a single line executes, same as a missing bracket — if you want to see partial output before a crash, you need an error the compiler can't catch in advance.


The Four Programs

For each one, run it. Then write down:

  • Which kind of mistake — compiler, runtime, or logic
  • What is actually wrong
  • The corrected line

Bug 1

Symptom: this program refuses to build at all.

Bug 1
double price = 19.99;
int quantity = 3;
Console.WriteLine($"Total: {price * quantity}";

Extra question: which line and column does the compiler name? Check it against where the mistake actually is.


Bug 2

Symptom: it builds, runs, and prints Average: 206. The average of 88, 92, and 79 should be about 86.3.

Bug 2
int score1 = 88;
int score2 = 92;
int score3 = 79;
Console.WriteLine($"Average: {score1 + score2 + score3 / 3}");

Hint: the number is far too big for an average of three scores under 100. Which part of that line does C# do first? And once the parentheses are right, will the answer still show a decimal — or do you need one more thing from Activity 1.6?


Bug 3

Symptom: it builds, runs, and prints Whole hours: 20. There are 3 whole hours in 200 minutes.

Bug 3
int totalMinutes = 200;
int wholeHours = totalMinutes % 60;
Console.WriteLine($"Whole hours: {wholeHours}");

The operator being used is not wrong in general — it is just answering a different question than the one asked. What question is it answering?


Bug 4

Symptom: === ROSTER === prints, and then the program crashes. Note that some output appeared before it died — that alone tells you this is not a compiler error.

Bug 4
Console.WriteLine("=== ROSTER ===");
int crewCount = 7;
int shifts = 0;
int perShift = crewCount / shifts;
Console.WriteLine($"Crew per shift: {perShift}");

Read every line before the crash. One of these variables never gets a value that would make the last line safe to run.


Submit

For each of the four bugs, submit:

  • The kind of mistake — compiler, runtime, or logic
  • One sentence on what was wrong
  • The corrected line of code

Plus your answer to Bug 1's extra question about line and column.

Two of these four are logic errors. If you found yourself waiting for an error message on those, that is exactly the habit this challenge is meant to break.

Commence Debugging