Beginner - How to find and type i, 2 times in a table

Sleaze

New member
Joined
May 10, 2011
Messages
1
Programming Experience
Beginner
The Project: We have ten athletes and the user defines the time they did. Those times are saved in a table called times(10). Clicking a button calculates the minimum time, the maximum and the average time. Also it finds the place of (i) in the table and sends it to a TextBox.

The Problem:
What if we have 2 same minimum times, for example (10 sec and 10 sec) or 2 maximum times (20 sec and 20 sec), in the same table. How are we supposed to send to the textbox 2 places of (i)?

The Code:


VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim max As Double
        Dim min As Double
        Dim avg As Double
        Dim i As Double

        max = times(1)
        For i = 1 To 10 Step 1
            If max < times(i) Then
                max = times(i)
                If times(i) = max Then
                    Place2.Text = i
                End If
            End If
        Next
        Last.Text = max
        


        min = times(1)
        For i = 1 To 10 Step 1
            If min > times(i) Then
                min = times(i)
            End If
            If times(i) = min Then
                Place1.Text = (i)
            End If
        Next
        First.Text = min


        avg = times(0)
        For i = 0 To 10 Step 1
            avg = avg + times(i)
        Next
        avg = avg / 10
        MO.Text = avg

    End Sub

Untitled copy.jpg
Picture related above with translation
Any help will be appreciated. I feel like it's something too simple and i just can't seem to find it.
Thanks in advance, Sleazy
 
Last edited:
One way you can do it is to create another loop and go through all your times and count how many times the max time repeats after you found your max time. Then simply just report that count with the max number like "2 people won 1st place"

so after this line of code

VB.NET:
        max = times(1)
        For i = 1 To 10 Step 1
            If max < times(i) Then
                max = times(i)
                If times(i) = max Then
                    Place2.Text = i
                End If
            End If
        Next
        Last.Text = max

do something like

VB.NET:
        Dim count As Integer = 0
        For Each time As Integer In times 'use a double if the values stored in your times array is of double type instead of an integer
            If time = max Then
                count += 1
            End If
        Next

        Last.Text = String.Format("{0} people finished in last place with a time of {1}.", count, max)

then just do the same thing for the first place


hope that helps...
 
Back
Top