Thread: AVG High/Low test scores

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    38

    AVG High/Low test scores

    I have an exercise to do and I am just starting. The exercise asksto write a program that will prompt the user to list test scores and then print the lowest, highest and the average of the test scores. Each test score is an integer greater than or equal to 0. Now the user terminates the list by entering ANY negative number. Pretty simple right? lol well not for a NOOB! ANyways Hers what I started with but as of now I am only trying to enter the numbers and get the break by entering a negative number and then show the average. I havent even attempted to print high/low yet. The problem is that I cant get out of the loop so when I start my program i cant get out of it by using negative numbers. Thanks!
    Code:
    main()
    {
        int i, num, avg, high, low;
        
        printf("Enter test scores (-1 to terminate list)\n");
        for (i=1; i>=0; i++)
        {
            scanf("%d", &num);
            i+=num;
        if (i<0)
        {
            break;
        }
        i = i+1;
        }
        printf("Average: %0.2f\n", num/i);
    }

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    First off I'll give you the little speech almost everyone who posts here gets:
    - main is defined either int main(void) or int main(int argc, char *argv[])
    - indent your code properly!

    As for your problem, you should probably not use a for loop, which you try to exit with a break; statement. Think about what you want to do here, you want to loop and keep getting numbers while the input is not negative. In general, the for loop is used when you want to loop over something a known number of times.

    Another little mistake in your code is that you use i both as a counter, and as the sum of all the test scores. When you check if i < 0 as an exit condition, you are checking if the sum of all the test scores is negative.You need to keep a separate counter for the number of test scores, and the sum of the test scores.

    One more thing is that you increase i both in the beginning and the end of the for loop: for (i=1; i>=0; i++) and later: i = i+1; For each entered score, i will increase with two.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Ok thanks for the tip our teacher never told us that part about main. I completely understand Im sure you guys get annoyed with all the noobs asking questions! Ok so I made some changes and got the negative to work and get out of the loop! How does this look so far?

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

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    How would be best to average? Normaly I would make an integer and ask the user how many test scores they will enter and then take the scores/number of test scores. So Im not sure how to do it without asking.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by BeldenML View Post
    How would be best to average? Normaly I would make an integer and ask the user how many test scores they will enter and then take the scores/number of test scores. So Im not sure how to do it without asking.
    Aren't you getting any compiler warnings? (hint: always turn up the compiler warnings to max) In your printf statement you are telling it you want to print a float (or double) but both num and i are ints. Either way, when you divide two integers by each other, any decimal fraction will be truncated, so if num/i is 3.7, the result will be 3. Consider using a float variable for the average instead.

    When you calculate the average, you divide num by i, rather than sum, probably just a typo. i = i++ is undefined behaviour, you should use either ++i; or i = i +1; or i += 1. And please change your definition of main.

    What would happen to the sum if the user enters a (large) negative value? Think about where you check for negative user input. Add a printf-statement in the end printing the sum, and enter 5 as the first score, and exit with -20 and see what happens.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    From your post #3, I see:
    Code:
    i=1;
    while (num>=0)
    {
        scanf("%d", &num);
    It would be better to write:
    Code:
    i = 0;
    while (scanf("%d", &num) == 1 && num >= 0)
    {
    The reason why I suggest starting i at 0 is that this makes i a count of the number of test scores read thus far. Of course, this means that if the list is empty, you will divide by 0, so you need to include a check for that. Also, notice that I check the return value of scanf. This is good because even though a negative number is supposed to be used to end the list, the list could also end simply because there are no more numbers left to read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    ice away first its funny because our teacher specifically noted that we should use "i++" He also told us that we did not need to add "void" in between main. I dont know why hes teaching us the wrong way but Im sure you know what you are talking about and I will use it the correct way. I also now realize after I dded the printf statement that youa re exactly correct in saying that the -1 is adding into my overall score. Here is the code I have now.

    Code:
    int main(void)
    {
        float i;
        int num,sum, avg, high, low;
        
        printf("Enter test scores (-1 to terminate list)\n");
        sum=0;
        i=0;
        while (1)
        {
            scanf("%d", &num);
            sum += num;
        if (num<0)
            {
                break;
            }
            i=i+1;
        }
        printf("Sum: %d\n",sum);
        printf("Average: %0.2f\n", sum/i);
    }
    Also Here is a test I ran
    Enter test scores (-1 to terminate list)
    50
    50
    50
    -1
    Sum: 149
    Average: 49.67

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    So how do I use the negative number to break the loop but not include it in adding to my sum? laserlight Im sry but Im not quite understanding what you are saying. I added what you suggested and when I ran a test my sum was no longer correct and neither was my avg? So im not sure what other checks I need to include as you have suggested.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BeldenML
    our teacher specifically noted that we should use "i++"
    Your teacher probably told you to use i++; not i = i++;

    Quote Originally Posted by BeldenML
    He also told us that we did not need to add "void" in between main.
    It is true that you don't need to because this is a function definition, but if this were merely a function declaration (e.g., a function prototype), then not having the void there means "number and types of parameters unknown". Hence, it is more consistent to always have the void there when you mean "no parameters".

    Quote Originally Posted by BeldenML
    So how do I use the negative number to break the loop but not include it in adding to my sum?
    You can use my suggestion

    Quote Originally Posted by BeldenML
    I added what you suggested and when I ran a test my sum was no longer correct and neither was my avg?
    What is your current code? The code you posted in post #7 is very different from code that incorporates what I suggested.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BeldenML View Post
    Ok thanks for the tip our teacher never told us that part about main. I completely understand Im sure you guys get annoyed with all the noobs asking questions! Ok so I made some changes and got the negative to work and get out of the loop! How does this look so far?

    Code:
    main()
    {
        int i, num,sum, avg, high, low;
        
        printf("Enter test scores (-1 to terminate list)\n");
        sum=0;
        i=1;
        while (num>=0)
        {
            scanf("%d", &num);
            sum+=num;
        if (num<0)
        {
            break;
        }
        i=i++;
        }
        printf("Average: %0.2f\n", num/i);
    }
    But you failed to correct your main() function...

    Out in the wild, programmers know that main() returns an errorlevel as an integer to the Operating System. Failing to do so may cause batch processing and some system calls to behave badly... it's not cosmetic.

    The minimum skeletal program in C is...
    Code:
    int main (void)
      {  
         // your muppingsharf here
    
        return 0;
    }
    Fix yours...

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Common I corrected it in my most recent code and in this one I am posting for laserlight. I think I found the solution to my problem I just added a sum-=num before the break so that way it adds whatever negative number the user enters to break the loop. So as of now I am able to input test scores terminate the program with any number and then print the average. My next step is just to print the high/low scores. Where should I start with that? laserlight will the code I have below work? I understand there are a multitude of different ways to write code and Im sure yours is much better than mine but I am still learning so if the way I have it will work I would like to keep it that way, but if it is not correct please let me know.

    Code:
    int main(void)
    {
        float i;
        int num,sum, avg, high, low;
        
        printf("Enter test scores (-1 to terminate list)\n");
        sum=0;
        i=0;
        while (1)
        {
            scanf("%d", &num);
            sum += num;
        if (num<0)
            {
                sum-=num;
                break;
            
            }
            i=i+1;
        }
        printf("Sum: %d\n",sum);
        printf("Average: %0.2f\n", sum/i);
    }
    Here is a trial run

    Enter test scores (-1 to terminate list)
    87
    54
    92
    73
    -20
    Sum: 306
    Average: 76.50

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BeldenML View Post
    Common I corrected it in my most recent code and in this one I am posting for laserlight. I think I found the solution to my problem I just added a sum-=num before the break so that way it adds whatever negative number the user enters to break the loop.
    Don't add stuff to negate problems... fix the problem!


    Code:
    int main(void)
    {
        float i;
        int num,sum, avg, high, low;
        
        printf("Enter test scores (-1 to terminate list)\n");
        sum=0;
        i=0;
        while (1)
        {
            scanf("%d", &num);
            if (num<0)
              break;
    
            sum += num;
            i++;
        }
    
        printf("Sum: %d\n",sum);
        printf("Average: %0.2f\n", sum/i);
        return 0;
    }
    The fix was as simple as moving the summing line below the breakout line.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Its funny I think Ive learned more working with you guys than from my professor!

  14. #14
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    I would suggest changing the contents of your while-loop a bit. First read the number, then check if it is below zero or not. If it is non-negative, add it to sum and increase the counter, if it is negative, exit the loop.

    I would not use i as a float value here, since it it just a counter and it does not need floating point precision. Instead I would make your avg variable a float, and before printing the average, calculate the average into the avg variable and print that instead.

    Code:
    float avg;
        ...
    avg = (float)sum/i;

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Well tater if I would have know that I probably would have done that..

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