Hey! I'm working on this solution that wants me to code an application that displays the highest score earned on the midterm and then the highest score on the final exam. This is waht I have so far but it is not working...

Option Explicit On
Option Strict On

Public Class MainForm

    Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
        Me.Close()
    End Sub

    Private Sub xDisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
        ' displays the highest score earned on the midterm 
        ' and the highest score earned on the final

        ' declare arrays and fill with data
        ' midterm scores are in the first column
        ' final scores are in the second column
        Dim scores(,) As Integer = {{89, 98}, _
                                    {78, 45}, _
                                    {67, 89}, _
                                    {90, 99}, _
                                    {91, 70}, _
                                    {75, 76}}
        Dim subscript As Integer
        Dim highestMidterm As Integer = scores(0, 0)
        Dim highestFinal As Integer = scores(0, 1)
        Dim midtermLabel As Integer
        Dim finalLabel As Integer


        ' search for highest values

        Do Until subscript = 6
            subscript = subscript + 1
            For subscript1 As Integer = 1 To 5
                If scores(subscript1, 0) > highestMidterm Then
                    highestMidterm = scores(subscript1, 0)
                End If
            Next subscript1

            For subscript2 As Integer = 1 To 5
                If scores(subscript2, 1) > highestFinal Then
                    highestFinal = scores(subscript2, 1)
                End If
            Next subscript2
        Loop

        ' display highest values
        midtermLabel = highestMidterm
        finalLabel = highestFinal

    End Sub
End Class

I'm having trouble getting this to work, I know there are some mistakes but I'm not sure where? Any solutions would be appreicated!

Recommended Answers

All 3 Replies

I'm having trouble getting this to work

Your code actually works i.e. midtermLabel and finalLabel variables will hold the desired values.

I know there are some mistakes

Your Do-loop is obsolete. The code finds the highest values in the For Next loops (in the first round) so you could remove Do and Loop lines and subscript variable.

If you want to display result in labels (assuming midtermLabel and finalLabel would be labels) then the correct syntax would be

'Dim midtermLabel As Integer REMOVE THIS LINE
'Dim finalLabel As Integer REMOVE THIS LINE
.
.
midtermLabel.Text = highestMidterm.ToString ' Assign value to a label
finalLabel.Text = highestFinal.ToString ' Assign value to a label

You could also change For Next statements from For subscript1 As Integer = 1 To 5 to For subscript1 As Integer = 1 To scores.GetUpperBound(0) and For subscript2 As Integer = 1 To 5 to For subscript2 As Integer = 1 To scores.GetUpperBound(0) . Now if you add a new score to scores matrix, the code would work without any modification.

HTH

i dnt think ur code works
cause u r not traversing the whole array
u r traversing only one column of each row.
first of all the for loop should run from 1 to 6
inititalize subscript value to 1
in the if statement

If scores(subscript1, subscript) > highestMidterm Then
highestMidterm = scores(subscript1, subscript)
End If

similar changes should be made in the second if statement

first of all the for loop should run from 1 to 6

Absolutely not. Arrays in VB.NET are zero based. Index value 6 would give an out-of-bounds error.

cause u r not traversing the whole array
u r traversing only one column of each row.

The first For Next loop loops first "column" and the second loops second "column". That's correct because first "column" holds mid-term scores which is checked in first loop. Second loop loops second "column" which holds final exam scores.

i dnt think ur code works

Don't think, copy/paste the code and test it ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.