8 Replies - 8341 Views - Last Post: 03 November 2010 - 05:35 PM Rate Topic: -----

#1 morketh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 02-November 10

Getting largest and smallest number from an array and placing in 2 dif

Posted 02 November 2010 - 05:34 PM

I have the code written for everything except when I try to display the highest and smallest numbers from the array into a label it display 0 for both the smallest and highest number.

Question: Create an application that lets the user enter 10 values into an array. The application should display the largest and smallest values stored in the array.

*The form is a list box with 2 labels and a button to retrieve the 10 numbers and put them in the list box then the other button puts the highest and lowest number in the 2 labels


(I am not sure how to display line numbers in my code on VB2010, please if you know tell me and I will edit the post)

Public Class Form1

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()

    End Sub

    Private Sub btnInputValues_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInputValues.Click
        'Create an array to hold the 10 numbers
        Const intMAX_SUBSCRIPT As Integer = 9       'The maximum subescript
        Dim intNumbers(intMAX_SUBSCRIPT) As Integer 'array declaration
        Dim intCount As Integer                     'Loop count

        'Tells the user what will happen
        MessageBox.Show("An input box will appear, please enter 10 numbers into the input boxes.")

        'get the numbers from the user
        For intCount = 0 To intMAX_SUBSCRIPT
            intNumbers(intCount) = InputBox("Enter a number.")
        Next

        'clear the list box of its current contents 
        lstValues.Items.Clear()

        'Display the contents of the array in the list box.
        For intCount = 0 To intMAX_SUBSCRIPT
            lstValues.Items.Add(intNumbers(intCount))
        Next
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Const intMAX_SUBSCRIPT As Integer = 9       'The maximum subescript
        Dim intNumbers(intMAX_SUBSCRIPT) As Integer 'array declaration
        Dim intCount As Integer                     'Loop count
        Dim intHighest As Integer       'to hold the highest value
        Dim intSmallest As Integer        'to hold the lowest value

        'get the first high value
        intHighest = intNumbers(intMAX_SUBSCRIPT)

        'search for the highest value.
        For intCount = 1 To (intNumbers.Length - 1)
            If intNumbers(intCount) > intHighest Then
                intHighest = intNumbers(intCount)
            End If
        Next

        lblHighest.Text = "The highest number is " & intHighest.ToString


        'get the first low value
        intSmallest = intNumbers(intMAX_SUBSCRIPT)

        'search for the lowest value
        For intCount = 1 To (intNumbers.Length - 1)
            If intNumbers(intCount) < intSmallest Then
                intSmallest = intNumbers(intCount)
            End If
        Next

        lblSmallest.Text = "The smallest number is " & intSmallest.ToString
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clear everything
        lstValues.Items.Clear()
        lblHighest.Text = String.Empty
        lblSmallest.Text = String.Empty
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Getting largest and smallest number from an array and placing in 2 dif

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 966
  • View blog
  • Posts: 3,721
  • Joined: 02-July 08

Re: Getting largest and smallest number from an array and placing in 2 dif

Posted 02 November 2010 - 06:40 PM

Well Array.Sort(<arrayname>) will sort it out for you. The 0 index will be the smallest and the last element will be the largest. Of course I prefer List(Of T) which can call it's Sort method and do the same.
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1998
  • View blog
  • Posts: 8,808
  • Joined: 29-May 08

Re: Getting largest and smallest number from an array and placing in 2 dif

Posted 03 November 2010 - 03:36 AM

that will sort the array. What if sorting the array is not a requirement?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1397
  • View blog
  • Posts: 4,494
  • Joined: 25-September 09

Re: Getting largest and smallest number from an array and placing in 2 dif

Posted 03 November 2010 - 04:23 AM

To find the highest, Initialize your intHighest at 0 (assuming you don't allow negative numbers)

the since you now know your highest number, initialize your intSmallest = intHighest and work though the code to find elements smaller than intSmallest.

You will also need to dim your array at the class level this way you can use it throughout the class, you are re-initializing it when you click the button for max and min. Which means the array is empty.

This post has been edited by CharlieMay: 03 November 2010 - 04:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 966
  • View blog
  • Posts: 3,721
  • Joined: 02-July 08

Re: Getting largest and smallest number from an array and placing in 2 dif

Posted 03 November 2010 - 06:48 AM

That is why it is called a suggestion AdamSpeight2008. Without knowing of your possible concern, my idea seems quite simple does it not?
Was This Post Helpful? 0
  • +
  • -

#6 morketh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 02-November 10

Re: Getting largest and smallest number from an array and placing in 2 dif

Posted 03 November 2010 - 07:17 AM

View PostCharlieMay, on 03 November 2010 - 03:23 AM, said:

To find the highest, Initialize your intHighest at 0 (assuming you don't allow negative numbers)

the since you now know your highest number, initialize your intSmallest = intHighest and work though the code to find elements smaller than intSmallest.

You will also need to dim your array at the class level this way you can use it throughout the class, you are re-initializing it when you click the button for max and min. Which means the array is empty.


Okay thank you, I will have to dim the array at the class level then try it out
Was This Post Helpful? 0
  • +
  • -

#7 morketh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 02-November 10

Re: Getting largest and smallest number from an array and placing in 2 dif

Posted 03 November 2010 - 04:48 PM

Alright thank you all for your help I almost have it working

Well the book gave me the example for that loop which is why I used it.

Alright I have a new problem. I have attached the code after declaring all of the variables at module level like you said.

When I runt he program it works!!! BUTTT! When I enter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 as my 10 numbers it shows the number 2 as being the lowest number and 10 being the highest number..

It is not even paying attention to the 1st entry. I enter 500 for the first entry then 2, 3, 4, 5, 6, 7, 8, 9 and it says 2 is the smallest number and 10 is the highest number.

So I believe there is something wrong with my coding in my 2 loops to find the high and low number but I cannot figure it out, it is ignoring the 1st number no matter what. I have attached my changes, but i think my problem is in the btnInputValues click event handler I am just not sure what to change :(

I have attached a picture below of the program and the problem

Posted Image

Uploaded with ImageShack.us


'Loop count
Public Class Form1
    Const intMAX_SUBSCRIPT As Integer = 9       'The maximum subescript
    Dim intNumbers(intMAX_SUBSCRIPT) As Integer 'array declaration
    Dim intCount As Integer                     'Loop count
    Dim intHighest As Integer       'to hold the highest value
    Dim intSmallest As Integer        'to hold the lowest value

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()

    End Sub

    Private Sub btnInputValues_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInputValues.Click
        'Create an array to hold the 10 numbers


        'Tells the user what will happen
        MessageBox.Show("An input box will appear, please enter 10 numbers into the input boxes.")

        'get the numbers from the user
        For Me.intCount = 0 To intMAX_SUBSCRIPT
            intNumbers(intCount) = InputBox("Enter a number.")
        Next

        'clear the list box of its current contents 
        lstValues.Items.Clear()

        'Display the contents of the array in the list box.
        For Me.intCount = 0 To intMAX_SUBSCRIPT
            lstValues.Items.Add(intNumbers(intCount))
        Next
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click



        'get the first high value
        intHighest = intNumbers(intMAX_SUBSCRIPT)

        'search for the highest value.
        For Me.intCount = 1 To (intNumbers.Length - 1)
            If intNumbers(intCount) > intHighest Then
                intHighest = intNumbers(intCount)
            End If
        Next

        lblHighest.Text = "The highest number is " & intHighest.ToString


        'get the first low value
        intSmallest = intNumbers(intMAX_SUBSCRIPT)

        'search for the lowest value
        For Me.intCount = 1 To (intNumbers.Length - 1)
            If intNumbers(intCount) < intSmallest Then
                intSmallest = intNumbers(intCount)
            End If
        Next

        lblSmallest.Text = "The smallest number is " & intSmallest.ToString
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clear everything
        lstValues.Items.Clear()
        lblHighest.Text = String.Empty
        lblSmallest.Text = String.Empty
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#8 morketh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 02-November 10

Re: Getting largest and smallest number from an array and placing in 2 dif

Posted 03 November 2010 - 04:54 PM

w00t!!! I GOT IT!!


'search for the lowest value
For Me.intCount = 1 To (intNumbers.Length - 1)
If intNumbers(intCount) < intSmallest Then
intSmallest = intNumbers(intCount)
End If
Next


I changed it to say For Me.intCount = 0 To (intNumbers.Length - 1)

Did the same thing for highest value loop
AWESOME!@#!@#!@#!@#
Was This Post Helpful? 1
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1397
  • View blog
  • Posts: 4,494
  • Joined: 25-September 09

Re: Getting largest and smallest number from an array and placing in 2 dif

Posted 03 November 2010 - 05:35 PM

LOL, Good Job!!!, Yes, Arrays start with an index of 0. I'm going to give you a + for figuring that out. Plus it will help anyone else that runs into the same issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1