2

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.

3 Answers 3

5

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

4
  • Beat me by a few seconds, and I like the way you worded it better anyway. Oct 21, 2008 at 13:16
  • I have to say ... I don't think I ever noticed the Take() method before. Neat!
    – John Rudy
    Oct 21, 2008 at 13:38
  • .Take() is an extension method on System.Linq.Enumerable. Check it (and more) out on msdn: msdn.microsoft.com/en-us/library/…
    – Amy B
    Oct 21, 2008 at 13:46
  • That's why I never noticed it -- I'm JUST NOW beginning to work with 3.5. :)
    – John Rudy
    Oct 21, 2008 at 14:43
3

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.