Dim GradeTotal(6) As Integer ' array to hold totals by grade

Dim GradeIn(20, 4) As Integer ' two dimensional array to hold 20 students * 4 assignments
' use re-dim if number of students increases

Dim TotalMarks(4) As Integer ' array to hold total marks for each assignment

Dim StudNo As Integer ' variable to hold number of students

Dim GradeOut(4) As String 'array for print out of grade for individual student

Dim HiStudGrade(4) As Integer ' array to hold student with highest grade

Dim LoStudGrade(4) As Integer ' array to hold student with lowest grade

Dim HiStudName(4) As String ' array to hold student name with highest grade for each assignment

Dim LoStudName(4) As String ' array to hold student name with lowest grade for each assignment


Private Sub Form_Load()

    For k = 0 To 3

        ' set lowest mark to 100 (this will be reset by first entry)
        LoStudGrade(k) = 100

    Next

End Sub


Private Sub Command1_Click()

    For k = 0 To 3

        ' increment the total marks for each assignment

        TotalMarks(k) = TotalMarks(k) + Val(StudMark(k))

        ' set the grade for each assignment mark
        ' and increment the totals for each grade

        GradeIn(StudNo, k) = Val(StudMark(k))

        Select Case GradeIn(StudNo, k)

            Case 0 To 29
                GradeOut(k) = "F"
                GradeTotal(5) = GradeTotal(5) + 1
            Case 30 To 39
                GradeOut(k) = "E"
                GradeTotal(4) = GradeTotal(4) + 1
            Case 40 To 49
                GradeOut(k) = "D"
                GradeTotal(3) = GradeTotal(3) + 1
            Case 50 To 59
                GradeOut(k) = "C"
                GradeTotal(2) = GradeTotal(2) + 1
            Case 60 To 69
                GradeOut(k) = "B"
                GradeTotal(1) = GradeTotal(1) + 1
            Case Is >= 70
                GradeOut(k) = "A"
                GradeTotal(0) = GradeTotal(0) + 1

            End Select

        'student with highest grade evaluated with each entry

        If Val(StudMark(k)) > HiStudGrade(k) Then

            HiStudName(k) = StudName
            HiStudGrade(k) = Val(StudMark(k))

        End If

        'student with lowest grade evaluated with each entry

        If Val(StudMark(k)) < LoStudGrade(k) Then

            LoStudName(k) = StudName
            LoStudGrade(k) = Val(StudMark(k))

        End If

    Next

    ' Caption giving student name and grades

    StudOut = StudName & " " & GradeOut(0) & " " & GradeOut(1) & " " & GradeOut(2) & " " & GradeOut(3)

    ' Clear the student name text box
    
    StudName.Text = ""

    ' increment number of students

    StudNo = StudNo + 1

    'must allow text boxes for inputting marks to be cleared - override non-numeric

    valid_char = ""

    For k = 0 To 3

        'clear text boxes for inputting marks

        StudMark(k) = ""

        ' set text boxes for highest, lowest and average marks

        HiGrade(k).Text = HiStudName(k)

        LoGrade(k).Text = LoStudName(k)

        AvMark(k).Text = Str(TotalMarks(k) / StudNo)

    Next

    For k = 0 To 5

        'transfer Grade Totals to relevant text boxes

        GradTot(k).Text = GradeTotal(k)

    Next

    ' transfer student total to relevant text box

    TotalStuds.Text = StudNo

        'give focus to relevant textbox for entry of next student

        StudName.SetFocus

End Sub

Private Sub studmark_change(Index As Integer)

    ' only allow numeric values to be entered
    ' values over 100 not allowed (negative sign not a valid character)
    ' but must override this restriction when marks are submitted (see above)

    valid_char = IsNumeric(StudMark(Index).Text)

    If Not valid_char Or Val(StudMark(Index).Text) > 100 Then

        Beep
        StudMark(Index).Text = ""

    End If

End Sub

Private Sub StudName_Change()

    ' clears caption for previous student when new name entered

    If StudName.Text <> "" Then

        StudOut.Caption = ""

    End If

End Sub

Back to vb6 exercises