One dimensional array.

Mike Bailey

Well-known member
Joined
Sep 26, 2003
Messages
57
Location
Renton, WA
Ok lets say I wanted to get a few students grades using a inputebox. I need to keep each grade in a variable and allow the user to enter up to 101 grades. Now lets say for what ever reason I need to use a array to do some other stuff later.
Code:
Dim myArray(101) as integer
So I would have a index of 0 to 100
How do I use the inputbox to give each index a value (the grade)

Thnks for any Help
Hopeless NewBeeeeeee
Mike Bailey
 
Actually, this will create an array with 102 elements, ranging from
0 to 101. If you wanted 100 elements, you would declare myArray
as
Code:
Dim myArray(99) As Integer

You can use a For-Each loop to loop through an array, so just
do that, take the users input, convert it to an Integer, and move
on.

Code:
Dim myArray(99) As Integer
Dim grade As Integer

For Each grade in myArray
  grade = Convert.ToInt32(InputBox("Grade?")) 
Next

Keep in mind this loop doesnt account for when a user enters a
value that is not an integer. You should also create your own
form for inputting values, as InputBox is one of those old VB6
functions that should be avoided in .NET.
 
Something like

Code:
dim max_grade as integer = 0
dim av_grade as integer = 0
dim min_grade as integer = 100

For Loop i = 0 to 100
  if myarray(i) > max_grade then maxgrade = myarray(i)
  if myarray(i) < min_grade then mingrade = myarray(i)
  av_grade = av_grade + myarray(i)
Next i

msgbox ("Max grade = " + str(max_grade))
msgbox ("Min grade = " + str(min_grade))
msgbox ("Average grade = " + str(av_grade / 100))

Although personally rather then set it at 100, i would use a variable to hold this number, then have the user enter the correct number into a txtbox, which defaults to 100. That way you cover more possibilities, in the future without rewriting the program.

Reading Buckys earlier reply, you would be better off using for each then for loop - forgot about them!

You can also put my if statements in buckys loop, to save running through it again and making it more efficient.
 
Back
Top