max and min of array

viper1314

Member
Joined
May 2, 2007
Messages
6
Programming Experience
Beginner
I am having trouble figuring out how to find the max and min values of an array. The index seems that it can be found , but not the actual valule.

I have an array full of % of performance of students so say my array is called ID(24) array 0 will be like 60%, 1 will be 80% 2 might be 40% Then I need to find the highest % and output that to a text box for performance tracking. I am really stuck on this one , been trying for the last 2 days to figure it out. does anyone has any tips or help on how to solve this ?
 
You can probably call the .Sort method on the array then get the lower bound value and the upper bound value.

Ex:

Dim ArrayOfInts() AsInteger
ArrayOfInts = NewInteger() {15, 2, 4, 5, 20, 7, 8, 1}
Array.Sort(ArrayOfInts)
MessageBox.Show(ArrayOfInts.GetValue(0).ToString())
MessageBox.Show(ArrayOfInts.GetValue(ArrayOfInts.GetUpperBound(0)).ToString())

But, I'm sure there are other (probably faster) ways.

CT
 
Last edited:
what does the ArrayOfInts = NewInteger() {15, 2, 4, 5, 20, 7, 8, 1} do in the program ?

I ask because something isn't matching up right and I think this is it. I just started working on arrays so I don't know all about them yet.


 
what does the ArrayOfInts = NewInteger() {15, 2, 4, 5, 20, 7, 8, 1} do in the program ?

I ask because something isn't matching up right and I think this is it. I just started working on arrays so I don't know all about them yet.

That just creates an array of integers for test purposes. If could have been an array of strings.

Dim ArrayOfStrings() AsString
ArrayOfStrings = NewString() {"fifteen", "two", "four", "five", "twenty", "seven", "eight", "one"}

This would have returned "eight" and the min value and "two" as the max value, since it's doing string comparison. Whereas the first example I provided would have returned a min of 1 and a max of 20.

Anyway, you would just call Array.Sort on your array if you already have it built.

Ex: Array.Sort(YourArrayHere)

If you provide some sample code I am sure we can help steer you in the right direction.

CT
 
Ok, that is what was wrong, I already had stuff in there, and this was making its own array elements.

Here is the code of what reflects the array........the project is 6 forms and 2 classes , so somestuff comes over from other forms , but should be able to follow along to get an idea of what is going on .

Private Sub newstudentButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newstudentButton.Click
summaryForm.Activate()
studentID(loopcountInteger) = summaryForm.completeaverage
If loopcountInteger = 24 Then
newstudentButton.Enabled = False
MessageBox.Show("This will be the last Student to play the game at this time", "Max Users", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else : Me.newstudentButton.Enabled = True
loopcountInteger += 1
TextBox1.Text = (loopcountInteger).ToString
ArithmeClass.arithclasstotaleventsinteger = 0 : ArithmeClass.arithclasstotalrightinteger = 0
moneyClass.moneyclasstotaleventsdecimal = 0 : moneyClass.moneyclasstotalrightDecimal = 0
End If




Then for the array data
Public Function completeaverage() As Decimal
'adds up the total right and wrong decimal from both classes and makes one average
CompleteTotalrightDecimal = (moneyClass.moneyclasstotalrightDecimal + ArithmeClass.arithclasstotalrightinteger)
CompleteTotaleventsDecimal = (moneyClass.moneyclasstotaleventsdecimal + ArithmeClass.arithclasstotaleventsinteger)
Averageinteger = (CompleteTotalrightDecimal / CompleteTotaleventsDecimal)
Return Averageinteger



End Function


Then I do the average for all the arrays, which isn't apart of the array , but only fills a textbox in the summary form.


The thing I am trying to use for the max value of the array is this

Public Function MaxValOfIntArray(ByRef studentID As Decimal) As Decimal
'This function gives max value of int array without sorting an array
MaxIntegersamount = 0
For i = 0 To UBound(welcomeForm.studentID)
If welcomeForm.studentID(i) > MaxValOfIntArray Then
MaxIntegersamount = i
End If
Next
' index of max value is MaxValOfIntArray
Return MaxIntegersamount
End Function



But I think it only returns an index, and I need an actual value such as "87%"

Then it fills up the textboxes in the summary.

Private Sub summaryForm_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try

currentaveTextBox.Text = completeaverage.ToString("P")
If welcomeForm.loopcountInteger > 0 Then
completeaveTextBox.Text = averageofAllarrays.ToString("P")
'Me.maxscoreTextBox.Text = Me.MaxValOfIntArray.tostring
Me.minscoreTextBox.Text = Me.minintegersindex.ToString
End If
Catch
MessageBox.Show("Not Enough Information for Summary", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub

Hopefully someone can figure this out , I can explain anything that doesn't seem right. Everything is working , except max and min


thanks
Joe
 
PublicFunction MaxValOfIntArray(ByRef studentID AsDecimal) AsDecimal
'This function gives max value of int array without sorting an array
MaxIntegersamount = 0
For i = 0 To UBound(welcomeForm.studentID)
If welcomeForm.studentID(i) > MaxValOfIntArray Then
MaxIntegersamount = i
EndIf
Next
' index of max value is MaxValOfIntArray
Return MaxIntegersamount
EndFunction

I'm going to have to look at your code better a little later, but the first thing I see wrong is the indicated line in function above. The MaxIntegersamount = i statement will always return your position in the for..next loop, not the value of the items you are looping through.

Based on your code, it looks like you need to change that line to MaxIntegersamount = welcomeForm.studentID(i), but as I said I haven't gone through the code in detail yet.

You may want to set a break point at that line and see what the value of welcomeForm.studentID(i) is.

After I look at the code I'll get back to you.

CT
 
Thanks for helping me out,

I replaced that line of code like you said and it started working better, but its giving the value of the max index.

So like if array 0 is 60 , array 1 is 100 , array 2 is 30 its returning array 2 value of 30 since it is the higest index.
 
Still not working, I thought it was but now its just returning the first index value ...


Public Function MaxValOfIntArray() As Decimal '(ByRef studentId As Decimal) As Decimal
'This function gives max value of int array without sorting an array
Dim b As Decimal
MaxIntegersamount = 0
For b = 0 To UBound(welcomeForm.studentID)
If welcomeForm.studentID(i) > MaxValOfIntArray Then
MaxIntegersamount = welcomeForm.studentID(i)
End If
Next
' index of max value is MaxValOfIntArray
' MaxValOfIntArray
Return MaxIntegersamount
End Function
 
Last edited:
If welcomeForm.studentID is actually a true array, then your functions should be as simple as:

VB.NET:
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] MinValOfIntArray() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE]
[SIZE=2] Array.Sort([/SIZE][SIZE=2]welcomeForm[/SIZE][SIZE=2].studentID)[/SIZE]
[SIZE=2] Return [/SIZE][SIZE=2]welcomeForm[/SIZE][SIZE=2].studentID.GetValue(0)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] MaxValOfIntArray() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE]
[SIZE=2] Array.Sort([/SIZE][SIZE=2]welcomeForm[/SIZE][SIZE=2].studentID)[/SIZE]
[SIZE=2] Return [/SIZE][SIZE=2]welcomeForm[/SIZE][SIZE=2].studentID.GetValue([/SIZE][SIZE=2]welcomeForm[/SIZE][SIZE=2].studentID.GetUpperBound(0))[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]

CT
 
That appears to be working, the last thing you did there.

I just need to put input for all 25 array fiels to make sure , but the max is working so I don't see why the min wouldn't work either.

Thanks a ton man , array stuff is all greek to me right now
 
That appears to be working, the last thing you did there.

I just need to put input for all 25 array fiels to make sure , but the max is working so I don't see why the min wouldn't work either.

Thanks a ton man , array stuff is all greek to me right now

You're welcome and good luck.

CT
 
Back
Top