3 Replies - 610 Views - Last Post: 09 October 2015 - 04:05 AM Rate Topic: -----

#1 Saicreed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-October 14

Largest / Smallest Array Values

Posted 08 October 2015 - 04:51 AM

Alright, so I've only got one problem left to solve. I need to figure out how to get the actual numbers to display in the list box besides all 0's. I've reviewed a previous post from 2010 in regards to this for help, but my professor wants us using the function instead of just calling upon a message box within InputValues_Click. I've tried using an extra return statement with "temp", but that did nothing. Right now, I always get nothing but 0's in the list box. Furthermore, my book only shows me how to list arrays in a message box with no guidance on list boxes. I would be grateful for any assistance in solving this!

Public Class LargestSmallestArrayValues

    Const MAX_NUMBERS As Integer = 10
    Dim Numbers(MAX_NUMBERS) As Integer
    Dim Count As Integer

    Private Sub btnInputValues_Click(sender As Object, e As EventArgs) Handles btnInputValues.Click

        For Me.Count = 1 To MAX_NUMBERS
            Me.GetSingle("Please enter value #" & Me.Count & " of " & MAX_NUMBERS & ":")
        Next
        lbInput.Items.Clear()
        For Me.Count = 1 To MAX_NUMBERS
            lbInput.Items.Add(Numbers(Count))
        Next
    End Sub

    Private Function GetSingle(ByVal prompt As String) As Single

        Dim sngTemp As Single = 0

        ' If the user enters an invalid value, the loop repeats
        ' and asks them to input the value again.

        Do While (True)
            Dim temp As String = InputBox("Must be numeric.", prompt)

            ' If the user clicks the Cancel button, return 0.

            If temp.Length = 0 Then Return 0

            ' If the input can be converted to a number, return
            ' its value to the caller.

            If Single.TryParse(temp, sngTemp) Then
                Return sngTemp
            End If
        Loop
    End Function

    Private Sub btnDisplayValues_Click(sender As Object, e As EventArgs) Handles btnDisplayValues.Click

        Dim Highest As Integer
        Dim Smallest As Integer

        Highest = Numbers(MAX_NUMBERS)
        For Me.Count = 0 To (Numbers.Length - 1)
            If Numbers(Count) > Highest Then
                Highest = Numbers(Count)
            End If
        Next
        lblHighest.Text = "The highest number is: " & Highest.ToString

        Smallest = Numbers(MAX_NUMBERS)
        For Me.Count = 0 To (Numbers.Length - 1)
            If Numbers(Count) < Smallest Then
                Smallest = Numbers(Count)
            End If
        Next
        lblLowest.Text = "The smallest number is: " & Smallest.ToString

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lbInput.Items.Clear()
        lblHighest.Text = String.Empty
        lblLowest.Text = String.Empty
    End Sub

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

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Largest / Smallest Array Values

#2 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1460
  • View blog
  • Posts: 3,703
  • Joined: 01-February 13

Re: Largest / Smallest Array Values

Posted 08 October 2015 - 12:20 PM

I am not quite sure what you are asking. However, i can see some things in your code that are not right.

To start with, an Array is zero indexed, meaning it`s first element starts at 0, not 1. So, you are declaring an Array that has 11 elements instead of 10.

If you want the Array to hold 10 numbers, then you need to declare it like this. 0 is the 1st number and 9 would be the 10th number.
Dim Numbers(MAX_NUMBERS - 1) As Integer


Then in your btnInputValues click event sub, you want to change your loops to start at 0 and go to (MAX_NUMBERS - 1). You also need to add the number returned from the GetSingle function to the Array.

This is where your problem is, i believe. You are not assigning the values that the user types, into the elements of the Array.
    Private Sub btnInputValues_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnInputValues.Click
        For Count = 0 To MAX_NUMBERS - 1
            Numbers(Count) = GetSingle("Please enter value #" & (Count + 1).ToString & " of " & MAX_NUMBERS.ToString & ":")
        Next
        lbInput.Items.Clear()
        For Count = 0 To MAX_NUMBERS - 1
            lbInput.Items.Add(Numbers(Count))
        Next
    End Sub



Since you are filling an Array of Integer type numbers, you will also need to change your GetSingle function so that it converts the String from the InputBox to an Integer, not a Single. The return type of the function also needs to be changed to an Integer type.
    Private Function GetSingle(ByVal prompt As String) As Integer 'change to Integer

        Dim sngTemp As Integer = 0

        ' If the user enters an invalid value, the loop repeats
        ' and asks them to input the value again.

        Do While (True)
            Dim temp As String = InputBox("Must be numeric.", prompt)

            ' If the user clicks the Cancel button, return 0.

            If temp.Length = 0 Then Return 0

            ' If the input can be converted to a number, return
            ' its value to the caller.

            If Integer.TryParse(temp, sngTemp) Then
                Return sngTemp
            End If
        Loop
    End Function



The last thing you need to fix is these two lines from your btnDisplayValues click event sub. To get the last number in the Array, they also need to be (MAX_NUMBERS - 1).
Highest = Numbers(MAX_NUMBERS - 1)
'and
Smallest = Numbers(MAX_NUMBERS - 1)


I believe if you fix those things, you will find that it works the way you want. 8)
Was This Post Helpful? 1
  • +
  • -

#3 Saicreed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-October 14

Re: Largest / Smallest Array Values

Posted 09 October 2015 - 03:45 AM

That worked! Thanks, IronRazer! I was able to use your advice to touch up my other program as well. Sorry I didn't get to try this and reply sooner; I've been working overtime this week since while it's available.
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1460
  • View blog
  • Posts: 3,703
  • Joined: 01-February 13

Re: Largest / Smallest Array Values

Posted 09 October 2015 - 04:05 AM

You`re Welcome, glad it helped. 8)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1