By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,502 Members | 1,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,502 IT Pros & Developers. It's quick & easy.

average with items in an array

P: 18
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:

Expand|Select|Wrap|Line Numbers
  1. public Class btnAdd
  2.     Dim grades() As Double
  3.  
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         Dim name As String = InputBox("please enter a student's name.")
  7.         'validate name
  8.         Do While Not isvalidname(name)
  9.             name = InputBox("please enter a student's name.")
  10.         Loop
  11.         'display info
  12.         lstStudents.Items.Add(name)
  13.  
  14.         'validate grade
  15.         Dim grade As String = InputBox("please enter a positive number for grades.")
  16.         Do While Not IsPositiveNumber(grade)
  17.             grade = InputBox("please enter a positive number for grades.")
  18.         Loop
  19.         'populate array
  20.         Dim i As Integer
  21.         Dim size As Integer = lstStudents.Items.Count
  22.         ReDim grades(size - 1)
  23.         For i = 0 To grades.GetUpperBound(0)
  24.             grades(i) = CDbl(grade)
  25.         Next
  26.     End Sub
  27.     Function getaverage(ByVal grades() As Double) As Double
  28.         'validate input
  29.         Dim sum As Double = 0
  30.         Dim i As Double
  31.         'loop to find the average 
  32.         For i = 0 To grades.GetUpperBound(0)
  33.             sum = sum + grades(i) / lstStudents.Items.Count
  34.         Next
  35.         'return average 
  36.         Return MsgBox("The average is " & FormatNumber(sum))
  37.  
  38.     End Function
  39.  
  40.     Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click
  41.         'call getaverage function
  42.         getaverage(grades)
  43.     End Sub
Nov 16 '08 #1
Share this Question
Share on Google+
2 Replies


P: 18
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:

Expand|Select|Wrap|Line Numbers
  1. Public Class btnAdd
  2.     Dim grades() As Double
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.  
  6.         Dim name As String = InputBox("please enter a student's name.")
  7.         'validate name
  8.         Do While Not isvalidname(name)
  9.             name = InputBox("please enter a student's name.")
  10.         Loop
  11.         'display info
  12.         lstStudents.Items.Add(name)
  13.  
  14.         'validate grade
  15.         Dim grade As String = InputBox("please enter a positive number for grades.")
  16.         Do While Not IsPositiveNumber(grade)
  17.             grade = InputBox("please enter a positive number for grades.")
  18.         Loop
  19.         'populate array
  20.         Dim i As Integer
  21.         Dim size As Integer = lstStudents.Items.Count
  22.         ReDim Preserve grades(i)
  23.         For i = 0 To grades.GetUpperBound(0)
  24.             grades(i) = CDbl(grade)
  25.         Next
  26.     End Sub
  27.  
  28.     Function getaverage(ByVal grades() As Double) As Double
  29.         'validate input
  30.         Dim sum As Integer
  31.         Dim i As Integer
  32.         'loop to find the average 
  33.  
  34.         For i = 0 To grades.GetUpperBound(0)
  35.             ReDim Preserve grades(i)
  36.             sum = sum + Val(grades(i)) / lstStudents.Items.Count
  37.         Next
  38.         'return average 
  39.         Return MsgBox("The average is " & FormatNumber(sum))
  40.  
  41.     End Function
  42.  
  43.     Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click
  44.         'call getaverage function
  45.         getaverage(grades)
  46.     End Sub
Nov 17 '08 #2

lotus18
100+
P: 866
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
Nov 18 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.