Exercise 6 of 10 · Arrays
Chess Club Weekly Attendance
What you will make
A program that stores a week of chess club attendance counts in an array and prints each night's count plus the total.
The one new idea: Storing multiple values in an array and looping over it safely by index
Arrays let you group related values together and process them all with one loop instead of a separate variable for every single value.
Go straight to the code ↓Arrays let you group several related values under one name instead of inventing a new variable for each one. int[] attendance = { 9, 12, 7 } creates an array of a fixed size, and you reach each value by its index, starting at 0 — so attendance[0] is the first value. An array's .Length tells you how many elements it holds, and looping for (int i = 0; i < someArray.Length; i++) is the standard, safe way to visit every valid index without going past the end.
Worked example
Here's an array of three quiz attempt scores, with the best one picked out:
int[] attempts = { 62, 78, 71 };
int best = attempts[0];
for (int i = 0; i < attempts.Length; i++)
{
Console.WriteLine($"Attempt {i + 1}: {attempts[i]}");
if (attempts[i] > best)
{
best = attempts[i];
}
}
Console.WriteLine($"Best attempt: {best}");Attempt 1: 62
Attempt 2: 78
Attempt 3: 71
Best attempt: 78The loop's condition, i < attempts.Length, stops right after visiting the last valid index, which here is 2 since Length is 3.
Your turn
The starter code stores a week of chess club attendance in an array and prints each night's count alongside its total. Its loop condition is i <= attendance.Length, which lets i reach a value equal to Length itself — one past the last valid index — and that crashes the program when it tries to read attendance[5] from a 5-element array. Fix the condition so the loop only visits valid indices.
If something goes wrong
An IndexOutOfRangeException almost always means a loop condition used <= with .Length where it needed < (or started counting from the wrong place). If the program runs but the total looks wrong, check that you're not skipping the first or last element — trace through which indices i actually takes for a small array before trusting the sum.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
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
- Using <= with .Length instead of <.
- Valid indices only go up to Length - 1, so allowing i to equal Length causes an IndexOutOfRangeException on the last iteration.
- Assuming two related arrays stay lined up by index automatically.
- They only match up if both are built and updated together in the same order — nothing enforces that automatically.
- Forgetting array indices start at 0.
- The first element is array[0], not array[1], which throws off both the loop bounds and any manual index math.
Want a blank editor instead? Open the C# playground.