Reputation: 1
I put comments on the average output since I kept getting error messages about that. My out keeps saying:
Maximum value: 33
Minimum value: 33
What am I doing wrong?
Option Explicit On
Option Strict On
Module MinMax
Sub Main()
' Declare a named constant for array size here
Const MAX_NUMS As Integer = 10
' Declare array here
Dim numbers() As Integer = {33, 12, -6, 1001, 57, -1, 999, 365, 921, 724}
'Dim num1 As Integer
'Dim num2 As Integer
' Use this integer variable as your loop index
Dim loopIndex As Integer = 0
' Use this variable to store the number input by user
Dim value As Integer
' String version of number input by user
Dim valueString As String
' Use these variables to store the minimim and maximum values
Dim min As Integer
Dim max As Integer
' Use these variables to store the total and the average
Dim total As Double
Dim average As Double
' Write a loop to get values from user and assign to array
For loopIndex = 0 To MAX_NUMS - 1
valueString = InputBox$("Enter a value: ")
value = Convert.ToInt32(valueString)
' Assign value to array
'num1 = loopIndex + 1
'num2 = loopIndex - 1
Next loopIndex
' Assign the first element in the array to be the minimum and the maximum
min = numbers(0)
max = numbers(0)
' Start out your total with the value of the first element in the array
total = numbers(0)
' Write a loop here to access array values starting with numbers(1)
'For loopIndex = 0 To MAX_NUMS - 1
' loopIndex(0) + MAX_NUMS = max
' loopIndex(0) - MAX_NUMS = min
' Next loopIndex
' Within the loop test for minimum and maximum values
' Also accumulate a total of all values
' Calculate the average of the 10 values
average = loopIndex / MAX_NUMS
' Print the values stored in the numbers array
' Print the maximum value, minimum value, and average
System.Console.WriteLine("Maximum value: " & max)
System.Console.WriteLine("Minimum value: " & min)
' System.Comsole.WriteLine("Average = " & average)
End Sub
End Module
Upvotes: 0
Views: 10569
Reputation:
Your code is strange. You have a constant-initialized array, yet you seem to want the user to input the array values, If that's the case, you should modify the first loop like this
For loopIndex = 0 To MAX_NUMS - 1
valueString = InputBox$("Enter a value: ")
value = Convert.ToInt32(valueString)
' Assign value to array
numbers(loopIndex) = value
Next loopIndex
Next you want to get min, max and avg number, this part of your code is fine:
min = numbers(0)
max = numbers(0)
' Start out your total with the value of the first element in the array
total = numbers(0)
Then you have commented the loop that should do the actual work. You could write it like this
' We start loop with index 1 since index 0 is already accounted for
For loopIndex = 1 To MAX_NUMS - 1
If numbers(loopIndex) < min Then min = numbers(loopIndex)
If numbers(loopIndex) > max Then max = numbers(loopIndex)
' accrue total value
total = total + numbers(loopIndex)
Next loopIndex
' After the loop ends, the minimum value and the maximum value will be correctly
' placed in min and max respectively
' Now compute average
average = total / MAX_NUMs
Hope this is clear enough. Good Luck!
Upvotes: 2
Reputation: 218847
Removing the code that doesn't affect the output, you're left with this:
Option Explicit On
Option Strict On
Module MinMax
Sub Main()
Dim numbers() As Integer = {33, 12, -6, 1001, 57, -1, 999, 365, 921, 724}
Dim min As Integer
Dim max As Integer
min = numbers(0)
max = numbers(0)
System.Console.WriteLine("Maximum value: " & max)
System.Console.WriteLine("Minimum value: " & min)
End Sub
End Module
Basically, you're:
In all of your other code, at no point do you modify the array or the two integers you're outputting.
Upvotes: 2