?
Solved

largest/smallest Array Value

Posted on 2007-10-06
9
Medium Priority
?
2,483 Views
Last Modified: 2013-11-26
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
Comment
Question by:twoshortm
  • 4
  • 3
  • 2
9 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 20029860
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 64

Expert Comment

by:Fernando Soto
ID: 20029866
I replaced this line of code in my post

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

to what you had it;

    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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 20030429
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))
            numbers.Add(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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:twoshortm
ID: 20038756
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 64

Expert Comment

by:Fernando Soto
ID: 20040789
To whom are you directing your question to Idle_Mind or myself?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20041241
I don't think it matters Fernando...all the info is there...she just needs to modify it.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 20041377
Idle_Mind; I just am not sure whose code she is working from.
0
 

Author Comment

by:twoshortm
ID: 20043078
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 86

Expert Comment

by:Mike Tomlinson
ID: 20043177
To add something to a ListBox:
 
    ListBox1.Items.Add("something")

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question