Ok, so I'm a n00b at VB, and I'm taking an online class, and I have an assignment to make an application that finds the highest values in a two-dimmensional array. the textbook has a section about searching in two-dimensional arrays and finding the highest value in a one-dimensional array...but not the highest in a two-dimensional arr
ay. So I kind o tried to combine the two...to no avail. I have a feeling I'm either close or completely off the mark (which is more likely)
can anyone help?
Here's what I have so far:
Private Sub displayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles displayButton.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)
' search for highest values
Do Until subscript = 6
subscript = subscript + 1
For subscript As Integer = 1 To 5
If scores(subscript, 0) > highestMidterm Then
highestMidterm = scores(subscript, 0)
End If
Next subscript
For subscript As Integer = 1 To 5
If scores(subscript, 1) > highestFinal Then
highestFinal = scores(subscript, 1)
End If
Next subscript
Loop
' display highest values
midtermLabel.Text = highestMidterm.ToString
finalLabel.Text = highestFinal.ToString
End Sub
And the error message it give says "Variable 'subscript' hides a variable in an enclosing block"
help please?
Comments
: an assignment to make an application that finds the highest values
: in a two-dimmensional array. the textbook has a section about
: searching in two-dimensional arrays and finding the highest value in
: a one-dimensional array...but not the highest in a two-dimensional
: arr
: ay. So I kind o tried to combine the two...to no avail. I have a
: feeling I'm either close or completely off the mark (which is more
: likely)
: can anyone help?
:
: Here's what I have so far:
:
: Private Sub displayButton_Click(ByVal sender As Object, ByVal e As
: System.EventArgs) Handles displayButton.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)
:
: ' search for highest values
:
: Do Until [red]subscript[/red] = 6
: subscript = subscript + 1
: For [red]subscript As Integer[/red] = 1 To 5
: If scores(subscript, 0) > highestMidterm Then
: highestMidterm = scores(subscript, 0)
: End If
: Next subscript
:
: For subscript As Integer = 1 To 5
: If scores(subscript, 1) > highestFinal Then
: highestFinal = scores(subscript, 1)
: End If
: Next subscript
: Loop
:
: ' display highest values
: midtermLabel.Text = highestMidterm.ToString
: finalLabel.Text = highestFinal.ToString
:
: End Sub
:
:
:
: And the error message it give says "Variable 'subscript' hides a
: variable in an enclosing block"
:
: help please?
:
You cannot use the same variables as a counter in 2 different nested loops.
: loops.
Yes, well I tried that...I tried just a Do-Loop, just a For...next, just an If...Then, and many combinations. I guess what I'm asking is what is the right....combination I guess to make this work?
: : loops.
:
: Yes, well I tried that...I tried just a Do-Loop, just a For...next,
: just an If...Then, and many combinations. I guess what I'm asking is
: what is the right....combination I guess to make this work?
:
:
:
You need to have a [b]different[/b] variable name for each of your loop's counters. Thus the Do Until loop's variable [b]must[/b] be different from the For do loop's variable. In your code they are the same (see red parts).