Minimum function

I'm working on a project that has the user input 15 grades, calculates the average, and finds the maximum and minimum values. The average and maximum calculations work but I can't figure out how to get the minimum value. Here is my code so far:

Public Sub DetermineClassAverage()

Dim total As Integer ' sum of grades entered by user

Dim gradeCounter As Integer ' number of grades input

Dim grade As Integer ' grade input by user

Dim average As Integer ' average of grades

Dim max As Integer

Dim min As Integer

' initialization phase

total = 0 ' set total to zero

gradeCounter = 1 ' prepare to loop

' processing phase

While gradeCounter <= 15 ' loop 15 times

' prompt for and input grade from user

Console.Write("Enter grade: ") ' prompt for grade

grade = Console.ReadLine() ' input the next grade

total += grade ' add grade to total

gradeCounter += 1 ' add 1 to gradeCounter

If grade > max Then

max = grade

End If

End While

' termination phase

average = total \ 15 ' integer division yields integer result

' display total and average of grades

Console.WriteLine(vbCrLf & "Total of all 15 grades is " & total)

Console.WriteLine("Class average is " & average)

Console.WriteLine("The maximum value is " & max)

Console.WriteLine("The minimum value is " & min)

End Sub ' DetermineClassAverage

End Class ' GradeBook

I know finding the minimum should be easy but if anyone could help it would be great!



Answer this question

Minimum function

  • Jebrew

    as Tada said or just set it = to the first iteration and then check for less than the first value

    While gradeCounter <= 15 ' loop 15 times

    ' prompt for and input grade from user

    Console.Write("Enter grade: ") ' prompt for grade

    grade = Console.ReadLine() ' input the next grade

    total += grade ' add grade to total

    If grade > max Then

    max = grade

    End If

    If (gradeCounter=0) or (grade<min) then

    min = grade

    End if

    gradeCounter += 1 ' add 1 to gradeCounter

    End While



  • ECHS BACHS

    It's still not working. It keeps returning 0 or a number that was not the minimum value.
  • heavenlycharmus

    Did you rearrange the code as shown....especially putting the counter incement at the end of the loop...

    I did test the code and it works on this end...lets see exactly what you have

    I also noticed that you start the counter with 1 and not 0...that being the case you would have to change your logic to:

    If (gradeCounter=1***) or (grade<min) then

    *****Change it from =0 to =1

     



  • Syed Faraz Mahmood

    Use the same logic as the max variable...

    If grade > max Then

    max = grade

    End If

    If grade < min then

    min = Grade

    end if



  • MaggieChan

    I've tried and it doesn't work. It's always going to return a value of zero because when you declare min, visual basic automatically sets the value to zero.
  • RMB775

    So when you declare it, initialize it to Integer.MaxValue
  • Minimum function