Solved

# largest/smallest Array Value

Posted on 2007-10-06
2,259 Views
I am using Visual basic.net 2005 with windows XP. I am writing a program that needs to find the largest/smallest array values. The user has to enter 10 numbers and then have the numbers displayed. After that the largest and smallest value are displayed.  This is what i know i need to put in but i am not exactly sure on how to get started.
Dim numbers(50)  as Integer
Dim count As Integer
dim highest as integer
highest = numbers
for count  = 1 To (numbers.Length -1)
If numbers(count) > highest Then
highest = numbers(count)
end if
next count

I know to find the smallest is similar.
thank you lisa
0
Question by:twoshortm

LVL 62

Accepted Solution

Hi lisa;

This will do what you need. You were almost there.

Dim numbers() As Integer = {22, 12, 33, 74, 9, 18, 1, 99, 44, 2}
Dim count As Integer
' Initialize the lowest and highest values as shown below
Dim lowest As Integer = Integer.MaxValue
Dim highest As Integer = Integer.MinValue
' You can not assign an array to a integer value, so this statement
' is illegal
' highest = numbers
' In the following For loop when using count to index into the array
' remember that arrays start at 0 and not 1
For count = 0 To (numbers.Length - 1)
If numbers(count) > highest Then
highest = numbers(count)
End If
If numbers(count) < lowest Then
lowest = numbers(count)
End If
Next count

MessageBox.Show("Highest value is " & highest.ToString() & Environment.NewLine & _
"Lowest value is " & lowest.ToString())

Fernando
0

LVL 62

Expert Comment

I replaced this line of code in my post

Dim numbers() As Integer = {22, 12, 33, 74, 9, 18, 1, 99, 44, 2}

Dim numbers(50)  as Integer

And fill the array in with the values from the user. I did the above just to test the code.
0

LVL 85

Assisted Solution

Why not something like?...

Dim numberToCollect As Integer = 10
Dim numbers As New List(Of Integer)

Dim number As Integer
Dim enteredValue As String
For curNumber As Integer = 1 To numberToCollect
Do
enteredValue = InputBox("Value:", "Enter Number " & curNumber & " of " & numberToCollect)
Loop While enteredValue.Trim.Length = 0 OrElse (Not Integer.TryParse(enteredValue, number))
Next

numbers.Sort()
Dim minimum As Integer = numbers(0)
Dim maximum As Integer = numbers(numbers.Count - 1)
MessageBox.Show("Minimum: " & minimum & vbCrLf & "Maximum: " & maximum, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information)

If you absolutely need to use an Array then:

Dim numberToCollect As Integer = 10
Dim numbers(numberToCollect - 1) As Integer

Dim number As Integer
Dim enteredValue As String
For curNumber As Integer = 1 To numberToCollect
Do
enteredValue = InputBox("Value:", "Enter Number " & curNumber & " of " & numberToCollect)
Loop While enteredValue.Trim.Length = 0 OrElse (Not Integer.TryParse(enteredValue, number))
numbers(curNumber - 1) = number
Next

Array.Sort(numbers)
Dim minimum As Integer = numbers(0)
Dim maximum As Integer = numbers(numbers.Length - 1)
MessageBox.Show("Minimum: " & minimum & vbCrLf & "Maximum: " & maximum, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information)
0

Author Comment

Yes that works but what if you want to display the numbers the user enters in a listbox and can't you display the min and max results as a label.
0

LVL 62

Expert Comment

To whom are you directing your question to Idle_Mind or myself?
0

LVL 85

Expert Comment

I don't think it matters Fernando...all the info is there...she just needs to modify it.
0

LVL 62

Expert Comment

Idle_Mind; I just am not sure whose code she is working from.
0

Author Comment

from both of you guys. Both solution work but i wanted to know how to display the numbers the user entered into a listbox and then display the min/max value in a label.
0

LVL 85

Expert Comment

To add something to a ListBox:

To set a Label:

Label1.Text = "set"

We can't do everything for you.  I feel badly enough as this looks like homework now that I think about it..
0

## Featured Post

### Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.