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)
 
That's the same just the code is little longer and hmm probably clearer.
Except that you can not only speed it up by not converting it twice, you can also make the code shorter which is easier to read. Here's an example using this thread's demo code:
VB.NET:
Dim ScoreOne, ScoreTwo, ScoreThree As Double 

Double.TryParse(Score1.Text, ScoreOne) 'If fails ScoreOne is still 0.0R
Double.TryParse(Score2.Text, ScoreTwo) 'If fails ScoreTwo is still 0.0R
Double.TryParse(Score3.Text, ScoreThree) 'If fails ScoreThree is still 0.0R

'If you need to know whether the conversion failed, check the output of TryParse

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.
Well i always use IIF.
IIf() uses Object for both input's & the output, which means you have to cast the output to the desired type, whereas If() is strongly typed, so both inputs have to be of the same type as the output.
VB.NET:
Dim SomeValue As Double = CDbl(IIf(SomeExpression, 123.0, 456.0))
Dim OtherValue As Double = If(SameExpression, 123.0, 456.0)
Not to mention the If() is short circuited whereas IIf() evaluates everything both ends every time.
 
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.

The Double.TryParse method overload that accepts a NumberStyle can throw an Exception...

Double.TryParse Method (String, NumberStyles, IFormatProvider, Double) (System)

I bring this up because of that custom parsing method I was writing (and you subsequently assisted in) was giving me issues with exceptions from the TryParse method.
 
The Double.TryParse method overload that accepts a NumberStyle can throw an Exception...

Double.TryParse Method (String, NumberStyles, IFormatProvider, Double) (System)

I bring this up because of that custom parsing method I was writing (and you subsequently assisted in) was giving me issues with exceptions from the TryParse method.
Weird but true, exception to the rule? Nah, they do throw for NumberStyles and consider those cases programmers responsibility, unlike the string input that can be used directly from user input without programmers validation. You could say 'never say never', but that could perhaps more precisely be never throw exceptions for input strings and common usage. Also notice that they only throw for styles that is not right and never can be, and not for styles the happen to conflict a regular parse operation of input.
  • Example 1: a composite numeric value that does not reflect a styles combination is never right, if programmer handles enumeration values correctly this will never happen.
  • Example 2: a hex string according to MS can never reflect a Double value, so hex style is never right for that. Similar logical style rules exist for other data types.
  • Example 3: you could have string input with leading white and a number, but for reasons you have excluded AllowLeadingWhite style, here TryParse will not throw an exception. It is not because of that style the conversion fails, but because of invalid (to that rule) user input. If next string input didn't have leading white the conversion would succeed, so method call is inherently ok but the string input sometimes is not. 'It is not me, it's you'.
Conclusion? The TryParse methods throws for invalid function calls, but neither for invalid data input nor for conversions errors.
 
What you need is both a counter and an accumulator. Here is some pseudocode:

count = 0
accum = 0
Do
count = count + 1
Repeat 3 times
Enter 3 grades for student # count
Find average for student # count
accum = accum + average
Stop the loop when all students have been entered.
Divide the accum by the count to get the class average.
 
Back
Top