Skip to content

Exercise 10 of 10 · Putting It Together

Summer Camp Swim Test Results

What you will make

A program that goes through a list of campers' swim test lap counts, labels each as 'cleared' or 'not yet', and prints a summary report.

The one new idea: Combining lists, loops, and conditionals while avoiding integer-division truncation

Most real programs aren't just one concept in isolation — they combine loops, conditionals, and collections together, which is exactly what this final exercise pulls together.

Go straight to the code ↓

This last exercise doesn't add a totally new topic so much as it asks you to combine everything so far — a List<T>, a for loop that walks two lists together by index, an if that sets a status and updates a running count, and .Sum() from LINQ — while getting one more subtlety exactly right: integer division. When both sides of a / are int, C# performs integer division and throws away the remainder before the result is ever stored anywhere else, even in a double variable.

Worked example

Here's the same trap with a class's quiz scores:

C#
List<int> scores = new List<int> { 70, 85, 91, 60 };
int totalScore = scores.Sum();
double averageWrong = totalScore / scores.Count;
double averageRight = (double)totalScore / scores.Count;

Console.WriteLine($"Wrong way: {averageWrong}");
Console.WriteLine($"Right way: {averageRight}");
Output
Wrong way: 76
Right way: 76.5

totalScore / scores.Count is 306 / 4, computed entirely with int math first, giving 76 with the .5 thrown away, and only afterward does that truncated 76 get converted to a double for storage. Casting the numerator to double first, with (double)totalScore, forces the division itself to happen in floating point, keeping the .5.

Your turn

The starter code walks a camp's swim test results, labels each camper 'cleared' or 'not yet' based on a required lap count, and then tries to compute the average number of laps completed across everyone. The line double averageLaps = totalLaps / lapsCompleted.Count; divides two int values first, truncating the result, before it ever becomes a double. Fix it so the division itself happens in floating point.

If something goes wrong

If your average always comes out as a whole number when you know the data isn't evenly divisible, that's the integer-division trap — look for a / between two int-typed values and cast one of them to double before the division happens, not after. If the cleared/not-yet labels look wrong, double check the comparison uses >= against the required lap count, not >, since campers who complete exactly the required number should still count as cleared.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Assigning an int division result to a double variable and assuming that fixes the truncation.
The division happens first, based on the operand types at that point, before the result is ever assigned to anything.
Casting the result of the division instead of one of its operands.
By the time the division has already happened as integer division, the fractional part is already gone — casting afterward can't bring it back.
Forgetting that .Sum() on a List<int> returns an int.
That's exactly why totalLaps and lapsCompleted.Count being divided together triggers integer division in the first place.

Want a blank editor instead? Open the C# playground.