views:

6068

answers:

5

Hey, I need to find the min and max vaule on 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

btw my arrays are set at global level

thanks

+4  A: 

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)).

Jon Skeet
Beat me by a few seconds, and I like the way you worded it better anyway.
Joel Coehoorn
I have to say ... I don't think I ever noticed the Take() method before. Neat!
John Rudy
.Take() is an extension method on System.Linq.Enumerable. Check it (and more) out on msdn: http://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods.aspx
David B
That's why I never noticed it -- I'm *JUST NOW* beginning to work with 3.5. :)
John Rudy
A: 

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.

Joel Coehoorn
A: 

Are you sure 0 isn't in the array?

IAmCodeMonkey
A: 

Jon Skeet Thanks that works. Joel my grade_enter is at in global " Public grade_enter(20) As Integer"

I have tried to set it so it only uses the set amount of space it needs but I couldnt get that working

Thanks

If Jon Skeet's answer is the one you think is most helpful, you might want to click the checkmark next to it; that would make it your "accepted" answer. :)
John Rudy
+1  A: 

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.

John Rudy