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

New Topic/Question
Reply




MultiQuote







|