please help me

This is a discussion on please help me within the C++ Programming forums, part of the General Programming Boards category; Yes, but if you look at his code... Code: do{ i++; MyData >> hope.id >> hope.numgrade; hope.average = hope.numgrade/i; He's ...

  1. #16
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Yes, but if you look at his code...
    Code:
    do{
    i++;
    MyData >> hope.id >> hope.numgrade;
    hope.average = hope.numgrade/i;
    He's reassigning average by dividing each individual grade by the counter. He should be summing the grades, then dividing that by the counter.

  2. #17
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    Code:
    0 1 0 0 1 --> total = 2
    
    1  2  3  4  5  --> i = 5       average = 2/5 = 0
    The point here is that it will not work for all possible cases and thus is sloppy code.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I don't see how that's possible since the scores are going to be 30, 50, 70, 90, etc., so a running total of scores should always be greater than the number of students.
    Well, this was my first thought as well, which is why I pointed out the uninitialized variable. In my experience, uninitialized variables tend to take on astronomically huge values, and therefore the result of the division:
    100 (or even 200, assuming you're a maniac brainiac) / (astronomically huge) -> 0.00

    is not entirely unexpected.

    Besides, AH_Tze (not the original poster I'll admit) has already noticed and mostly corrected (aside from redundancy problems) the problematic average-calculating formula.
    Last edited by Hunter2; 02-10-2005 at 06:34 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    He's reassigning average by dividing each individual grade by the counter. He should be summing the grades, then dividing that by the counter.
    Yes, you are right. I was thinking about how it should be, and not how the poster actually did it. They actually started off with this:

    hope.average = hope.average/hope.total;

    which will result in 0 everytime when you initialize hope.average with 0--regardless of the integer division problem. That formula for an average is a little disconcerting coming from a teacher.
    Last edited by 7stud; 02-10-2005 at 09:14 PM.

  5. #20
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    That formula for an average is a little disconcerting coming from a teacher.
    Well, back when I was in school, teachers also tought indentation

    -Edit-

    In fact, I'm fairly certain one of the first things we learned was how to make legible code - indenation and commenting.
    Last edited by Kaelin; 02-11-2005 at 08:28 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21