Visual Basic 2010- How to make arrays calculate min, max, and average?
-
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 This is what I have so far and I commented everything that didn't work but that statements are commented to show where to put things. My out keeps saying: Maximum: 33 Minimum: 33 I can not get it to change without getting an error any tips on what to do? Thanks
-
Answer:
I honestly have no idea what you were trying to do with the commented code, but isn't what you need something like the following? For loopIndex = 0 To MAX_NUMS - 1 If numbers(loopindex) > max then max = numbers(loopIndex) If numbers(loopindex) < min then min = numbers(loopIndex) Next loopIndex
Midnite at Yahoo! Answers Visit the source
Other answers
Why even use an array, a ListOf() is much better and min, max and average are already predefined methods in the ListOf.
AJ
Related Q & A:
- What are the object-oriented features of Visual Basic.NET?Best solution by msdn.microsoft.com
- Can you program Graphics with Visual Basic?Best solution by Yahoo! Answers
- What are the main parts of visual basic 2005?Best solution by msdn.microsoft.com
- How do you use the Contains Function in Visual Basic?Best solution by go4expert.com
- How do I output from one form to another in Visual Basic?Best solution by support.microsoft.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.