Thread: AVG High/Low test scores

  1. #31
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    This is what I have so far but its not working

    Code:
    int main(void)
    {
        float i;
        int num,sum, high, low, var;
        
        printf("Enter test scores (-1 to terminate list)\n");
        sum=0;
        i=0;
        low=0;
        high=0;
        while (1)
        {
            scanf("%d", &num);
            if (num >= high)
            {
                high=num;
            }
            if (num <= var)
            {
                low=num;
            }
            var=num;
                
            if (num<0)
            {
                break;
                sum += num;
            }
            
            i++;
        }
        printf("Sum: %d\n",sum);
        printf("High score: %d\n", high);
        printf("Low score:  %d\n", low);
        printf("Average: %0.2f\n", sum/i);
        return 0;
    }

  2. #32
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    but now my average isnt working and the lowest number its printing is the -1. I think I need to move some things around but not sure where...

  3. #33
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Oh i figured out why my avg was off. I needed to place my sum statement after the break statement. Now I have avg, high, and sum working. I just need to figure out low.

  4. #34
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    ok here i changed low = 100 but Im having the same problem where it is reading my exit negative number as the lowest number. Heres what I have now

    Code:
    int main(void)
    {
        float i;
        int num,sum, high, low, var;
        
        printf("Enter test scores (-1 to terminate list)\n");
        sum=0;
        i=0;
        low=100;
        high=0;
        while (1)
        {
            scanf("%d", &num);
            if (num >= high)
            {
                high=num;
            }
            if (num <= low)
            {
                low=num;
            }    
            if (num<0)
            {
                break;
            }
            sum += num;
            i++;
        }
        printf("Sum: %d\n",sum);
        printf("High score: %d\n", high);
        printf("Low score:  %d\n", low);
        printf("Average: %0.2f\n", sum/i);
        return 0;
    }

  5. #35
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Yes! I think I finally have it working. I changed my low if statement that it must be greater than zero. This is my final draft so let me know what you guys think!

    Code:
    int main(void)
    {
        float i;
        int num,sum, high, low, var;
        
        printf("Enter test scores (-1 to terminate list)\n");
        sum=0;
        i=0;
        low=100;
        high=0;
        while (1)
        {
            scanf("%d", &num);
            if (num >= high)
            {
                high=num;
            }
            if (num <= low && num>=0)
            {
                low=num;
            }    
            if (num<0)
            {
                break;
            }
            sum += num;
            i++;
        }
        printf("Sum: %d\n",sum);
        printf("High score: %d\n", high);
        printf("Low score:  %d\n", low);
        printf("Average: %0.2f\n", sum/i);
        return 0;
    }

  6. #36
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    move the tests for high and low *below* your exit test... That is test for num < 0 FIRST, before you do anything else... now your code gets a lot simpler.

    I already showed you this when calculating the sum... why should high and low be any different?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. High Low Game with High Scores
    By Bradley Buck in forum C Programming
    Replies: 24
    Last Post: 05-27-2011, 12:42 PM
  2. High Scores - A Dilemma
    By CrazyNorman in forum Game Programming
    Replies: 5
    Last Post: 12-28-2006, 09:01 PM
  3. Solitaire High Scores
    By sean in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 07:15 PM
  4. Help on high scores
    By Gnoober in forum C++ Programming
    Replies: 0
    Last Post: 02-17-2003, 07:28 PM
  5. High Scores
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2003, 01:08 PM