Question Averages

machone

New member
Joined
Sep 12, 2010
Messages
2
Programming Experience
Beginner
Please bear with me--I'm a complete beginner.....I had to create a form that asks for a student's name and 3 test scores, and then (after clicking a button) displays the name and test average from the 3 scores in a label. That much is done and working. I also have to add a button that, when clicked, shows the class average---the average of all student averages combined and the number of students "processed". I know part of this is about counting but I don't know where to start.....

Sincerely,

Machone (Newbie)
 
well the class average would be the average of all student's averages.

So you get and store the average of students a, b, and c. These are your student averages. Then you add them up and divide by 3 (a,b,c, that's 3 students) and you have a class average.

I'd use a simple structure that stored all data representing a student (name, array of scores, average score). Every time a student is input you create this struct and set these values and stick them in some list or something. Then when the other button is hit you loop over these structs averaging all the student averages.



I'm not giving you any code because this sounds like a class homework assignment.
 
How to find average test score

take a look at the attached project (demo) ... generally speaking you just need to divide the sum of scores with the number of the tests (three in this situation) e.g.


VB.NET:
        Dim ScoreOne As Double = IIf(Double.TryParse(Score1.Text, 0), Convert.ToDouble(Score1.Text), 0)
        Dim ScoreTwo As Double = IIf(Double.TryParse(Score2.Text, 0), Convert.ToDouble(Score2.Text), 0)
        Dim ScoreThree As Double = IIf(Double.TryParse(Score3.Text, 0), Convert.ToDouble(Score3.Text), 0)
        Dim result As Double = Math.Round((ScoreOne + ScoreTwo + ScoreThree) / 3)
        ResultTextBox.Text = result.ToString

        ' optional
        MessageBox.Show(StudentName.Text & "'s average test score is: " & result.ToString)

        ' Now you can save the average score or something.
 

Attachments

  • AverageTestScores.zip
    33.6 KB · Views: 21
Take a look at the TryParse() method, the 2nd param is always the variable you want the value stored in if it's a successful parse.
VB.NET:
Dim YourValue As Double
If Double.TryParse("TheInput", YourValue) Then
    'YourValue contains the double that was parsed from "TheInput"
End If
And yes, in VS2008 and newer you should always use If() instead of IIf(), for multiple reasons :)
 
That's the same just the code is little longer and hmm probably clearer.
No, it's not the same. Here you convert/parse the input string twice like JB said:
If(Double.TryParse(Score1.Text, 0), Convert.ToDouble(Score1.Text), 0)
If TryParse succeeds it has already converted the value to Double, so why convert it again?
 
No, it's not the same. Here you convert/parse the input string twice like JB said:

If TryParse succeeds it has already converted the value to Double, so why convert it again?

Convert is checking for nothing too ?

However you can even do this if you don't care for 'nothing':
VB.NET:
Dim ScoreOne As Double = If(Double.TryParse(Score1.Text, ScoreOne), ScoreOne, 0)
 
Nothing is not an issue with TryParse, as its purpose is to not throw exceptions during conversion process.
 
But it does not check for nothing right?
If you mean if input value is Nothing, that is the first thing it checks, and if it is the conversion fails right there, which causes the function to return False. TryParse functions will never throw exceptions.
 
Back
Top