average with items in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jac130
    New Member
    • Oct 2008
    • 18

    average with items in an array

    I have an array, populated with student grades. there is a list of student names, but their grades are stored in a class level array. there is a calculate average button,that is supposed to calculate the grades in the array. But my button only returns the last grade that was entered, not the average, here is my code:

    Code:
    public Class btnAdd
        Dim grades() As Double
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim name As String = InputBox("please enter a student's name.")
            'validate name
            Do While Not isvalidname(name)
                name = InputBox("please enter a student's name.")
            Loop
            'display info
            lstStudents.Items.Add(name)
    
            'validate grade
            Dim grade As String = InputBox("please enter a positive number for grades.")
            Do While Not IsPositiveNumber(grade)
                grade = InputBox("please enter a positive number for grades.")
            Loop
            'populate array
            Dim i As Integer
            Dim size As Integer = lstStudents.Items.Count
            ReDim grades(size - 1)
            For i = 0 To grades.GetUpperBound(0)
                grades(i) = CDbl(grade)
            Next
        End Sub
        Function getaverage(ByVal grades() As Double) As Double
            'validate input
            Dim sum As Double = 0
            Dim i As Double
            'loop to find the average 
            For i = 0 To grades.GetUpperBound(0)
                sum = sum + grades(i) / lstStudents.Items.Count
            Next
            'return average 
            Return MsgBox("The average is " & FormatNumber(sum))
    
        End Function
    
        Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click
            'call getaverage function
            getaverage(grades)
        End Sub
    Last edited by debasisdas; Nov 17 '08, 05:46 AM. Reason: Formatted using code tags.
  • jac130
    New Member
    • Oct 2008
    • 18

    #2
    I'm not sure if i'm atucally populating the array, or it's jsut keeping the last value put into it. I tried the redim preserve keywords, but if i click the average button, it only gives me the average using the last grade entered,, not the sum of all of them. : here is my code for it:

    Code:
    Public Class btnAdd
        Dim grades() As Double
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim name As String = InputBox("please enter a student's name.")
            'validate name
            Do While Not isvalidname(name)
                name = InputBox("please enter a student's name.")
            Loop
            'display info
            lstStudents.Items.Add(name)
    
            'validate grade
            Dim grade As String = InputBox("please enter a positive number for grades.")
            Do While Not IsPositiveNumber(grade)
                grade = InputBox("please enter a positive number for grades.")
            Loop
            'populate array
            Dim i As Integer
            Dim size As Integer = lstStudents.Items.Count
            ReDim Preserve grades(i)
            For i = 0 To grades.GetUpperBound(0)
                grades(i) = CDbl(grade)
            Next
        End Sub
    
        Function getaverage(ByVal grades() As Double) As Double
            'validate input
            Dim sum As Integer
            Dim i As Integer
            'loop to find the average 
            
            For i = 0 To grades.GetUpperBound(0)
                ReDim Preserve grades(i)
                sum = sum + Val(grades(i)) / lstStudents.Items.Count
            Next
            'return average 
            Return MsgBox("The average is " & FormatNumber(sum))
    
        End Function
    
        Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click
            'call getaverage function
            getaverage(grades)
        End Sub
    Last edited by debasisdas; Nov 17 '08, 05:46 AM. Reason: Formatted using code tags.

    Comment

    • lotus18
      Contributor
      • Nov 2007
      • 865

      #3
      Are you sure you were adding the names in the listbox?
      OK. Here's the algo:
      1. Get the name of the student
      2. Get the grades of the student
      3. Compute for the average grade
      4. Proceed to the next student when you are done and follow steps 2-3 until the condition is met.
      5. Compute for the total average


      My 2nd option is create a temporary variable that will hold temporary values : )

      Rey Sean

      Comment

      Working...