Hi,
I have been working on this all weekend and can't figure out what I am doing wrong. The objective is to collect 3 numbers from the user then display to them the numbers they entered, the lowest number entered, the highest number entered, the total, and the average. Can someone review this and tell me what I am not getting? Thanks in advance!

Module Module1
    Const SIZE As Integer = 2
    Sub Main()
        Dim tempArray() As Integer = New Integer(SIZE) {}
        Dim entries() As Integer = New Integer(SIZE) {}
        Dim lowest, highest, total, average As Integer

        entries = GetEntries(tempArray)

        Message(entries)

        lowest = GetLowest(entries, SIZE)
        highest = GetHighest(entries, SIZE)
        total = GetTotal(entries, SIZE)
        average = GetAverage(entries, total, SIZE)

        Console.ReadLine()
    End Sub
    Function GetEntries(ByVal tempArray() As Integer) As Integer()
        For count = 0 To 2
            Console.WriteLine(" Please enter a whole number:")
            tempArray(count) = Console.ReadLine()
        Next
        Return tempArray
    End Function
    Sub Message(ByVal tempArray() As Integer)
        Console.WriteLine("Here are your entries: ")
        For count = 0 To 2
            Console.WriteLine(tempArray(count))
        Next
    End Sub
    Function GetLowest(ByVal entries() As Integer, ByVal SIZE As Integer) As Integer
        Dim index As Integer
        Dim lowest As Integer = 0
        lowest = entries(0)
        For index = 0 To entries(SIZE)
            If entries(index) < lowest Then
                Console.WriteLine()
                Console.WriteLine("This is the LOWEST number entered: " + CStr(entries(index)))
            End If
        Next
    End Function
    Function GetHighest(ByVal entries() As Integer, ByRef SIZE As Integer) As Integer
        Dim index As Integer
        Dim highest As Integer
        highest = entries(0)
        For index = 0 To entries(SIZE)
            If entries(index) > highest Then
                Console.WriteLine()
                Console.WriteLine("This is the LOWEST number entered: " + CStr(entries(index)))
                highest = entries(index)
            End If
        Next
    End Function
    Function GetTotal(ByVal entries() As Integer, ByVal SIZE As Integer) As Integer
        Dim index As Integer = 0
        Dim total As Integer = 0
        total = entries(0)
        For index = 0 To entries(SIZE)
            If entries(index) = +total Then
                Console.WriteLine()
                Console.WriteLine("This is the TOTAL for all entries: " + CStr(entries(index)))
            End If
        Next
    End Function
    Function GetAverage(ByVal entries() As Integer, ByVal total As Integer, ByVal SIZE As Integer) As Integer
        Dim index As Integer = 0
        Dim average As Integer = 0
        average = entries(0)
        For index = 0 To entries(SIZE)
            If entries(index) = average Then
                Console.WriteLine()
                Console.WriteLine("This is the AVERAGE of all numbers entered: " + CStr(entries(index)))
                average = total / (SIZE)
                Console.ReadLine()
            End If
        Next
    End Function
End Module

Recommended Answers

All 3 Replies

Hi,
I can see some issues with your lowest and highest functions. Your loop structure is wrong.

Function GetLowest(ByVal entries() As Integer, ByVal SIZE As Integer) As Integer
Dim index As Integer
Dim lowest As Integer = 0
lowest = entries(0)
For index = 0 To entries(SIZE)
If entries(index) < lowest Then
Console.WriteLine()
Console.WriteLine("This is the LOWEST number entered: " + CStr(entries(index)))
End If
Next
End Function

You set lowest to the 1st entry in the array and then check everything against it. Imagine you entered 5,7,9 to the array. Then 5 is not less than 5 so the message does not display at all. The first index can not be lower than itself. But, also, if you entered 13,7 4 to the array you would get the message displayed twice. 7 < 13 and 4 < 13.
Instead you need to run through the loop compairing each entry to the one above it and keeping the lowest.

Function GetLowest(ByVal entries() As Integer, ByVal SIZE As Integer) As Integer
Dim index As Integer
Dim lowest As Integer = 0
For index = 0 To entries(SIZE - 1)
If entries(index) < entries(index + 1) Then
   lowest = entries(index)
Else
   lowest = entries(index + 1)
End If
Next
Console.WriteLine()
Console.WriteLine("This is the LOWEST number entered: " + CStr(lowest))
End Function

The same logic needs to be applied to your highest function.

Hope that helps,

And your other functions are wrong too. I'm not sure whhy you have an If in your total function.

Function GetTotal(ByVal entries() As Integer, ByVal SIZE As Integer) As Integer
Dim index As Integer = 0
Dim total As Integer = 0
For index = 0 To entries(SIZE)
  total = total + entries(index)
Next
Console.WriteLine()
Console.WriteLine("This is the TOTAL for all entries: " + CStr(total))
End Function

You were returning an element from your array and saying that it was the total. How could it be?

And your average function should be:

Function GetAverage(ByVal entries() As Integer, ByVal total As Integer, ByVal SIZE As Integer) As Integer
Dim average As Integer
average = total/SIZE

Console.WriteLine()
Console.WriteLine("This is the AVERAGE of all numbers entered: " + CStr(average))
End Function

You had already calculated the total and passed it to this function so all that was left to do was the division by SIZE. And again you were returning an element of the array which, although possible,most likely would not be the average.

THANKS for everyone's help!! The final program has 20 user entries. Here is my finished and functioning program:

Module Module1
    Const SIZE As Integer = 20
    Sub Main()
        Console.SetWindowSize(50, 50)
        Dim tempArray() As Integer = New Integer(SIZE) {}
        Dim entries() As Integer = New Integer(SIZE) {}
        Dim lowest, highest, total As Integer

        entries = GetEntries(tempArray)

        Message(entries)

        lowest = GetLowest(entries)
        highest = GetHighest(entries)
        total = GetTotal(entries)
        GetAverage(entries, total)

        Console.ReadLine()
    End Sub
    Function GetEntries(ByVal tempArray() As Integer) As Integer()
        For count = 0 To 19
            Console.WriteLine(" Please enter a whole number:")
            tempArray(count) = Console.ReadLine()
        Next
        Return tempArray
    End Function
    Sub Message(ByVal tempArray() As Integer)

        Console.WriteLine("Here are your entries: ")
        For count = 0 To 19
            Console.WriteLine(tempArray(count))
        Next
    End Sub
    Function GetLowest(ByVal entries() As Integer) As Integer
        Dim index As Integer
        Dim lowest As Integer = 0
        lowest = entries(0)
        For index = 0 To SIZE - 1
            If entries(index) < lowest Then
                lowest = entries(index)
            End If
        Next
        Console.WriteLine()
        Console.WriteLine("This is the LOWEST number entered: " + CStr(lowest))
        Return lowest
    End Function
    Function GetHighest(ByVal entries() As Integer) As Integer
        Dim index As Integer
        Dim highest As Integer
        highest = entries(0)
        For index = 0 To SIZE - 1
            If entries(index) > highest Then
                highest = entries(index)
            End If
        Next
        Console.WriteLine()
        Console.WriteLine("This is the HIGHEST number entered: " + CStr(highest))
        Return highest
    End Function
    Function GetTotal(ByVal entries() As Integer) As Integer
        Dim counter As Integer
        Dim total As Double
        total = 0
        For counter = 0 To 19
            total += entries(counter)
        Next
        Console.WriteLine()
        Console.WriteLine("This is the TOTAL for all entries: " + CStr(total))
        Return total
    End Function
    Sub GetAverage(ByVal entries() As Integer, ByVal total As Integer)
        Dim index As Integer = 0
        Dim average As Integer = 0
        average = entries(0)
        average = total / (SIZE)
        Console.WriteLine()
        Console.WriteLine("This is the AVERAGE of all numbers entered: " + CStr(average))
        Console.ReadLine()
    End Sub
End Module
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.