Start learning free

LESSON 205 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Report Card V6 in C#: Highest, Lowest and Average of a List

Practice Read the full explanation

Compute three class statistics from a list of scores: the highest, the lowest, and the average.

You start with:

List<int> scores = new List<int> { 92, 78, 85, 67, 74, 88, 91, 58 };

Build:

Highest: 92
Lowest: 58
Average: 79

Rules:

  • highest is the largest value in scores.
  • lowest is the smallest value in scores.
  • average is the sum of all scores divided by how many there are, rounded down (integer division).
  • Print "Highest: ", "Lowest: " and "Average: " on three lines, in the order of the sample output (Console.WriteLine("Highest: " + highest);).

Use LINQ: scores.Max() and scores.Min() (add using System.Linq; if the editor needs it); sum with a loop.

List<int> scores = new List<int> { 92, 78, 85, 67, 74, 88, 91, 58 };

int highest = scores.Max();
int lowest = scores.Min();
int total = 0;
foreach (int s in scores) total += s;
int average = total / scores.Count;

Console.WriteLine("Highest: " + highest);
Console.WriteLine("Lowest: " + lowest);
Console.WriteLine("Average: " + average);

STEP 1 OF 8
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Report Card V6 in C#, explained

    Problem overview

    This version computes statistics for a whole class. The scores are in a List<int> named scores: 92, 78, 85, 67, 74, 88, 91 and 58. The program prints three lines: the highest score, the lowest score and the average, with the average rounded down to a whole number.

    Highest: 92
    Lowest: 58
    Average: 79

    The eight scores add up to 633, and 633 divided by 8 is 79.125, so the integer average is 79.

    Approach: two library calls and one running total

    The highest and lowest values are easy to get from the library. scores.Max() returns the largest element and scores.Min() the smallest. Both are LINQ methods that live in the System.Linq namespace, so a file needs using System.Linq; to call them on a list. They scan the whole list once, so you do not need to write a loop to find the extremes.

    The average needs the sum. Create an int named total that starts at 0, then add every score to it with a foreach loop. A running total always starts at zero, before any score has been counted. Then divide the total by the number of scores. For a List<T> the number of elements is the Count property.

    Both operands of the division are int, so C# truncates the result: 633 / 8 gives 79, and the fraction .125 is dropped. For a list of non-negative scores, truncation means rounding down, which is what the lesson asks for.

    Step by step

    1. Find the highest score with int highest = scores.Max();. One call, no loop.
    2. Find the lowest with int lowest = scores.Min();, the mirror of the first call.
    3. Create the running total. int total = 0; will hold the sum of all the scores.
    4. Add every score. foreach (int s in scores) total += s; visits each score in turn, 92, then 78, and so on. The loop body is a single statement, so it fits on one line without braces.
    5. Compute the average as total / scores.Count. Both are int, so the part after the point is dropped: the result is 79, not 79.125.
    6. Print three lines with Console.WriteLine: "Highest: " + highest, then "Lowest: " + lowest, then "Average: " + average, in that order.

    The solution in C#

            List<int> scores = new List<int> { 92, 78, 85, 67, 74, 88, 91, 58 };
    
            int highest = scores.Max();
            int lowest = scores.Min();
            int total = 0;
            foreach (int s in scores) total += s;
            int average = total / scores.Count;
    
            Console.WriteLine("Highest: " + highest);
            Console.WriteLine("Lowest: " + lowest);
            Console.WriteLine("Average: " + average);
    

    Details worth knowing:

    • Count or Length. A List<T> has Count, an array has Length. Mixing them up is a compile error.
    • The collection initializer. new List<int> { 92, 78, ... } creates the list and adds the values in one expression. Unlike an array, the list can grow later with Add.
    • LINQ can do more. scores.Sum() returns 633 here, and scores.Average() returns a double, 79.125 for this data. The lesson uses an explicit loop for the total so that the running-total pattern is clear, and an integer division for the average.
    • Shorthand in the loop. total += s is the same as total = total + s.

    Edge cases and common mistakes

    • An empty list. Max() and Min() on an empty list of int throw an InvalidOperationException with the message "Sequence contains no elements". The division total / scores.Count would fail too, with a DivideByZeroException, since the count is 0. The lesson's data always has scores.
    • A missing using. Without System.Linq in scope, scores.Max() does not compile, because a List<int> has no Max method of its own.
    • Not starting the total at 0. A total that starts at another value shifts the average. In C# a local variable must also be assigned before += reads it, so declaring it without a value is a compile error.
    • Dividing by the wrong count. Use scores.Count, not a number typed by hand, so the code keeps working when the list changes.
    • Expecting rounding to nearest. Integer division truncates. The scores 80, 80, 81, 81 and 81 add up to 403, and 403 / 5 is 80.6 exactly, but the program prints 80.

    Testing

    • The starting list prints Highest: 92, Lowest: 58 and Average: 79.
    • The list 90, 91 prints Highest: 91, Lowest: 90 and Average: 90, because 181 / 2 is 90.5, cut down to 90.
    • The one-element list 75 prints 75 on all three lines.
    • The list 80, 80, 81, 81, 81 prints Highest: 81, Lowest: 80 and Average: 80.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free