# I cant figure how to find the largest number in the array

Page 1 of 1

## 6 Replies - 12303 Views - Last Post: 17 August 2008 - 01:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=60859&amp;s=b663ca9d5abb3c47a3104919952c03e2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cnewman

Reputation: 0
• Posts: 14
• Joined: 15-September 07

# I cant figure how to find the largest number in the array

Posted 16 August 2008 - 01:28 PM

Hi my assignment is to calculate average and age and find the max age with their name. I have tried to search everywhere and came to the conclusion I need to run a for loop on the array. I dont want to sort because I also have to get the name of the person.

```Public Class frmNames

Private names() As String = {"Bob", "Carroll", "Ted", "Alice"}
Private age(3) As Integer

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStats.Click
'assign the ages
Me.GetAges()
'process the ages
Dim average As Decimal = Me.FigureAverageAge()
'display the results
Me.DisplayStats(average)
End Sub
'input
Private Sub GetAges()
age(0) = CInt(txtAge1.Text)
age(1) = CInt(txtAge2.Text)
age(2) = CInt(txtAge3.Text)
age(3) = CInt(txtAge4.Text)

End Sub
Private Function FigureAverageAge() As Decimal
Dim subTotal As Integer
'sum the ages
For index As Integer = 0 To age.GetUpperBound(0)
subTotal += age(index)
Next
Return CDec(subTotal / age.Length)
End Function
Private Function FigureOldest() As Integer

End Function

Private Sub DisplayStats(ByVal ageAverage As Decimal)
lblAge.Text = ageAverage.ToString

End Sub

Private Sub frmNames_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' the load event runs when the form is loaded into memory
txtName1.Text = names(0)
txtName2.Text = names(1)
txtName3.Text = names(2)
txtName4.Text = names(3)
End Sub

End Class

```

how do I find the oldest person in the array without sorting?

#### Attached File(s)

•  Names.zip (57.98K)

Is This A Good Question/Topic? 0

## Replies To: I cant figure how to find the largest number in the array

### #2 Jayman

• Student of Life

Reputation: 423
• Posts: 9,532
• Joined: 26-December 05

## Re: I cant figure how to find the largest number in the array

Posted 16 August 2008 - 01:47 PM

Well you are correct that it will require a loop. In the following example, you can see that I am using a For Each loop to iterate through the array.
You could use any type of loop to accomplish the same task. I just chose to use the For Each.

Once the loop has finished running you will have the index number of the element that is the oldest age, which you can then apply to the names array to get the name of the person who is the oldest.

```        Dim ages() As Integer = {12, 53, 14, 26, 35, 24, 39, 45}

'set the age of the first element for comparison
Dim oldest As Integer = ages(0)
'create a variable to hold the index, so that you can retreive the name
Dim indexOfOldest As Integer = 0
'create a counter to keep track of which element in the array you are comparing
Dim counter As Integer = 0
'now loop through all the ages in the array
For Each age As Integer In ages
'check if the current age is greater than the highest age
'that has been found so far
If oldest < age Then
'store the index of the element in the array
indexOfOldest = counter
End If
'increment the index
counter += 1
Next
```

### #3 cnewman

Reputation: 0
• Posts: 14
• Joined: 15-September 07

## Re: I cant figure how to find the largest number in the array

Posted 16 August 2008 - 02:36 PM

I wish there was a super thank you button. Your explanation was awesome and this helped me understand the process. Thank You * a million

### #4 Jayman

• Student of Life

Reputation: 423
• Posts: 9,532
• Joined: 26-December 05

## Re: I cant figure how to find the largest number in the array

Posted 16 August 2008 - 04:02 PM

You are very welcome. I am glad I was able to help.

While we don't have a super thank you button, in the lower right corner of each post is a "This Post Was Helpful!" button, which is for just that purpose.

### #5 cnewman

Reputation: 0
• Posts: 14
• Joined: 15-September 07

## Re: I cant figure how to find the largest number in the array

Posted 17 August 2008 - 10:11 AM

Hi im still getting errors in my code and im having a really hard time with the program. Im really hoping you can help!

Here is my code...
```Public Class frmNames

Private names() As String = {"Bob", "Carroll", "Ted", "Alice"}
Private age(3) As Integer

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStats.Click
'assign the ages
Me.GetAges()
'process the ages
Dim average As Decimal = Me.FigureAverageAge()
Dim oldest As Integer = Me.FigureOldest()
'display the results
Me.DisplayStats(average)
lblHighestAge.Text = CStr(oldest)

End Sub
'input
Private Sub GetAges()
age(0) = CInt(txtAge1.Text)
age(1) = CInt(txtAge2.Text)
age(2) = CInt(txtAge3.Text)
age(3) = CInt(txtAge4.Text)

End Sub
Private Function FigureAverageAge() As Decimal
Dim subTotal As Integer
'sum the ages
For index As Integer = 0 To age.GetUpperBound(0)
subTotal += age(index)
Next
Return CDec(subTotal / age.Length)
End Function
Function FigureOldest() As Integer
Dim oldest As Integer = age(0)
Dim indexOfOldest As Integer = 0
Dim counter As Integer = 0

For Each age As Integer In age
If oldest < age Then
indexOfOldest = counter
End If
counter += 1
Next
Return oldest
End Function

Private Sub DisplayStats(ByVal ageAverage As Decimal)
lblAge.Text = ageAverage.ToString

End Sub

Private Sub frmNames_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' the load event runs when the form is loaded into memory
txtName1.Text = names(0)
txtName2.Text = names(1)
txtName3.Text = names(2)
txtName4.Text = names(3)
End Sub

End Class

```

VB is telling me Expression is of type 'Integer', which is not a collection type.

Here is where it is saying it
```  Function FigureOldest() As Integer
Dim oldest As Integer = age(0)
Dim indexOfOldest As Integer = 0
Dim counter As Integer = 0

For Each age As Integer In [u]age[/u]<--- here, but this is my array name!
If oldest < age Then
indexOfOldest = counter
End If
counter += 1
Next
Return oldest
End Function

```

The syntax in my text book tells me
For Each elementName [As type] In arrayName
statments
Next

which is what I have done, and you have helped me understand, yet I am still stuck with this error any suggestions? Ill add this also to the public forum.

Thanks for all your help so far,
Christopher

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: I cant figure how to find the largest number in the array

Posted 17 August 2008 - 11:59 AM

The syntax in my text book tells me
For Each elementName [As type] In arrayName

elementName must be different to arrayName
```Public Class frmNames

Private names() As String = {"Bob", "Carroll", "Ted", "Alice"}
Private age(3) As Integer

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStats.Click
'assign the ages
Me.GetAges()
'process the ages
Dim average As Decimal = Me.FigureAverageAge()
Dim oldest As Integer = Me.FigureOldest()
'display the results
Me.DisplayStats(average)
lblHighestAge.Text = CStr(oldest)

End Sub
'input
Private Sub GetAges()
age(0) = CInt(txtAge1.Text)
age(1) = CInt(txtAge2.Text)
age(2) = CInt(txtAge3.Text)
age(3) = CInt(txtAge4.Text)

End Sub
Private Function FigureAverageAge() As Decimal
Dim subTotal As Integer
'sum the ages
For index As Integer = 0 To age.GetUpperBound(0)
subTotal += age(index)
Next
Return CDec(subTotal / age.Length)
End Function
Function FigureOldest() As Integer
Dim oldest As Integer = age(0)
Dim indexOfOldest As Integer = 0
Dim counter As Integer = 0

For Each this_age As Integer In age
If oldest < this_age Then
indexOfOldest = counter
End If
counter += 1
Next
Return oldest
End Function

Private Sub DisplayStats(ByVal ageAverage As Decimal)
lblAge.Text = ageAverage.ToString

End Sub

Private Sub frmNames_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' the load event runs when the form is loaded into memory
txtName1.Text = names(0)
txtName2.Text = names(1)
txtName3.Text = names(2)
txtName4.Text = names(3)
End Sub

End Class

```

This post has been edited by AdamSpeight2008: 17 August 2008 - 12:00 PM

### #7 Jayman

• Student of Life

Reputation: 423
• Posts: 9,532
• Joined: 26-December 05

## Re: I cant figure how to find the largest number in the array

Posted 17 August 2008 - 01:27 PM

As AdamSpeight2008 has indicated the problem is that your array name is the same as the element name in the For Each loop.

Because of the scope of the variable age, meaning it is declared in the function, it takes precedence over the array age that is declared outside of the function.

Either, rename the array of age to ages or rename the element name in the For Each loop to something other than age.