typestar

Grade report in Visual Basic

A complete program: query a dictionary, rank by average, letter it.

' Average quiz scores per student, then print a ranked report card.

Module GradeReport
    Sub Main()
        Dim scores As New Dictionary(Of String, Integer()) From {
            {"ada", {92, 88, 99}},
            {"grace", {85, 91, 78}},
            {"alan", {70, 82, 88}}
        }

        Dim ranked = From entry In scores
                     Let mean = entry.Value.Average()
                     Order By mean Descending
                     Select name = entry.Key, mean

        Console.WriteLine("student      mean  grade")
        For Each row In ranked
            Dim line = $"{row.name,-10}{row.mean,7:F1}  {LetterFor(row.mean)}"
            Console.WriteLine(line)
        Next
    End Sub

    Function LetterFor(mean As Double) As String
        If mean >= 90 Then Return "A"
        If mean >= 80 Then Return "B"
        Return "C"
    End Function
End Module

How it works

  1. Let mean = ... computes once and names the result inside the query.
  2. Select name = entry.Key, mean shapes anonymous result rows.
  3. A helper Function in the same Module turns means into letters.

Keywords and builtins used here

The run, in numbers

Lines
28
Characters to type
667
Tokens
158
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 118 seconds.

Type this snippet

Step 1 of 2 in Encore, step 28 of 29 in Language basics.

← Previous Next →