473,378 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

average with items in an array

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
2 5630
jac130
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
866 512MB
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

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

Similar topics

28
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
3
by: Tony Lennard | last post by:
I have several queries, which generate about 10 fields each a text field of length 2 (which contain effort and attainment grades), eg A2, B4, A3, A3. I am trying to calculate an 11th fields, which...
6
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
3
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
3
by: simpleeshelbee | last post by:
Hey guys, This is my second post and is URGENT!!!! My final assignment is due tonite for class and I have no idea how to write this program right! I am supposed to write a program that uses a...
4
by: gaga | last post by:
hi guys, a part of my program requires me to calculate an average of items that are sold. the easiest way to do that would be writing a function, but im having trouble making up the parameters. if...
0
by: JuAn2226 | last post by:
hi this my code Private Sub Form_Load() car_count = 0 Cumulative_Speed = 0 End Sub Private Sub Timer1_Timer() Dim tmpNumber As Integer
21
by: Bill Cunningham | last post by:
I have create these 2 files. Called main.c and atr.c. They seem to work pretty well. I just wanted to submit them to see what if any errors others that know more might find. Thanks. atr.c ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.