Reputation:
I need to find the min and max value in an array. The .max
function works but .min
keeps showing zero.
Public Class Program_2_Grade
Dim max As Integer
Dim min As Integer
Dim average As Integer
Dim average1 As Integer
Dim grade As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = Nothing Or TextBox1.Text > 100 Then
MsgBox("Doesn't Meet Grade Requirements", MsgBoxStyle.Exclamation, "Error")
TextBox1.Clear()
TextBox1.Focus()
counter = 0
Else
grade_enter(counter) = TextBox1.Text
TextBox1.Clear()
TextBox1.Focus()
counter = counter + 1
If counter = grade_amount Then
max = grade_enter.Max()
min = grade_enter.Min()
For i As Integer = 0 To counter
average = average + grade_enter(i) / counter
average1 = average1 + grade_enter(i) - grade_enter.Min / counter
Next
Select Case average
Case 30 To 49
grade = "C"
Case 50 To 69
grade = "B"
Case 70 To 100
grade = "A"
Case Else
grade = "Fail"
End Select
If (Program_2.CheckBox1.Checked = True) Then
Program_2.TextBox4.Text = _
("Name:" & " " & (Program_2.TextBox1.Text) & vbNewLine & _
"Class: " & (Program_2.TextBox2.Text) & vbNewLine & _
"Number Of Grades:" & " " & (Program_2.TextBox3.Text) & vbNewLine & _
"Max:" & " " & max & vbNewLine & _
"Min:" & " " & min & vbNewLine & _
"Average:" & " " & average1 & vbNewLine) & _
"Grade:" & " " & grade & vbNewLine & _
"Dropped Lowest Grade"
Else
Program_2.TextBox4.Text = _
("Name:" & " " & (Program_2.TextBox1.Text) & vbNewLine & _
"Class: " & (Program_2.TextBox2.Text) & vbNewLine & _
"Number Of Grades:" & " " & (Program_2.TextBox3.Text) & vbNewLine & _
"Max:" & " " & max & vbNewLine & _
"Min:" & " " & min & vbNewLine & _
"Average:" & " " & average & vbNewLine) & _
"Grade:" & " " & grade & vbNewLine
End If
Me.Close()
average = 0
average1 = 0
counter = 0
End If
End If
End Sub
My arrays are set at global scope.
Upvotes: 2
Views: 69555
Reputation: 37850
Stocksy101:
As others have mentioned, your array's initial values will be 0, so if you are creating an array larger than necessary, Min()
will always return 0.
Additionally, a cute little quirk of Visual Basic .NET is that when you declare an array such as:
Public grade_enter(20) As Integer
You're actually creating a 21-item array, not a 20-item array. (VB declares arrays as their upper bound.) (See StartVBDotNet.) So that might have something to do with it.
In any event, if you're on VB.NET 2005 or 2008, you might consider looking into the List(Of Integer)
class. (This is actually just the List
class; it's what's called "generic.") This class will allow you dynamically-sized arrays, which grow or shrink based on the items added to them. It doesn't, unfortunately, have Min()
and Max()
methods, but it does have a ToArray()
method, from which you could then run the Min()
and Max()
methods.
Upvotes: 3
Reputation: 415820
I'm having a hard time finding where you defined grade_enter(). That code would easier to read if you broke it up into a few smaller methods. But I'm guessing you defined it as an array of integers with a static size that's large enough to hold however many items your professor told you to expect. In that case, any unset item has a value of 0, which will be smaller than any grades that were entered. You need to account for that, perhaps by using a List(Of Integer)
rather than an array.
Upvotes: 0
Reputation: 1500665
You haven't shown where grade_enter is being created. My guess is that it's bigger than it needs to be, so there are "empty" entries (with value 0) which are being picked up when you try to find the minimum.
You could change it to:
max = grade_enter.Take(counter).Max()
min = grade_enter.Take(counter).Min()
as a hacky way of making it work, but it would be better to use the right amount of space to start with (or a List(Of Integer)
).
Upvotes: 5