Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

Largest and Smallest Array Values

Using Visual Basic, create an application that displays the largest and smallest values stored in a single dimensional array.  The user will enter ten integer values which can be negative or positive numbers.  When the user clicks on the “Input Values” button, an input box will appear prompting the user for a value.  If that value is a valid integer it will be displayed in the list box and a new input box will prompt the user for another value until all ten values are entered.  When the user clicks the “Display Min & Max” button, the values will be presented.  See the figure below for an example of what the program will look like at run time.

Largest Smallest Screenshot

When the program first runs, the label controls which display the largest and smallest values should initially be invisible.  They should become visible only when the “Display Min & Max” button is clicked.

Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
 frmLargestSmallestArray Largest / Smallest Array Form Holds Controls
 lstInputValues ListBox Displays values which have been input
 lblLargest Label Display max value. Visible property set to false.
 lblSmallest Label Display min value. Visible property set to false.
 btnInputValues &Input Values Button Trigger event to display min and max values
 btnClear  C&lear Button Triggers event to clear the list box and labels
 btnExit E&xit Button Triggers event to end program

Write the Code:

' Project:         Largest and Smallest Array Values
' Description:     Prompt the user to enter a list of ten integers.  
'     Display the largest and smallest values which have been entered.

Public Class frmLargestSmallestArray
    ' declare global variables
    Const ARRAY_SIZE = 10
    Dim numbers(ARRAY_SIZE - 1) As Integer
    Dim numberString As String = ""
    Dim valid As Boolean = True

    Private Sub btnInputValues_Click(sender As Object, e As EventArgs) Handles btnInputValues.Click
        ' handles the event for when the user clicks on the "Input Values" button 
        ' it calls input data sub routine to populate the list box
        lstInputValues.Items.Add("Input Values")
        InputData()
        For x = 0 To (ARRAY_SIZE - 1)
            lstInputValues.Items.Add(CStr(numbers(x)))
        Next
    End Sub

    Private Sub btnDisplayMinAndMax_Click(sender As Object, e As EventArgs) Handles btnDisplayMinAndMax.Click
        ' sub routine handles the event for when the user clicks on the "Display Min and Max" button
        lblSmallest.Text = ("The smallest number is " & CStr(smallestNumber()))
        lblSmallest.Visible = True
        lblLargest.Text = ("The largest number is " & CStr(largestNumber()))
        lblLargest.Visible = True
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        ' sub routine handles the event for when the user clicks on the "Clear" button.
        lstInputValues.Items.Clear()
        lblLargest.Text = ""
        lblSmallest.Text = ""
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        ' ends the program when the user clicks on the "Exit" button
        End
    End Sub

    Sub InputData()
        ' Validates input from user and assigns values to array
        For x = 0 To (ARRAY_SIZE - 1)
            Do
                valid = True ' Initialize to true to account for prior invalid input
                numberString = InputBox("Enter an integer value")
                If IsNumeric(numberString) Then
                    numbers(x) = CInt(numberString)
                    valid = True
                Else
                    MessageBox.Show("You did not enter a numeric value.  Please try again.")
                    valid = False
                End If
            Loop Until valid = True
        Next
    End Sub

    Function largestNumber() As Integer
        Dim largest As Integer = numbers(0)
        For x = 1 To (ARRAY_SIZE - 1)
            If (numbers(x) > largest) Then
                largest = numbers(x)
            End If
        Next
        Return largest
    End Function

    Function smallestNumber() As Integer
        Dim smallest As Integer = numbers(0)
        For x = 1 To (ARRAY_SIZE - 1)
            If (numbers(x) < smallest) Then
                smallest = numbers(x)
            End If
        Next
        Return smallest
    End Function
End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2025 · Metro Pro Theme on Genesis Framework · WordPress · Log in