Thread: Issue with Sum code

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    Issue with Sum code

    I have a program where I am trying to sum grades that are input by the end user. They can put in a max of 10 grades. The grades must be between 1-100. If they put in a number above or below they get an error message. When I run the program my "sum" command is adding in the grades that are in error. I was hoping someone could give me help?????

    Code:
    
    #include <stdio.h> 
    
    
        int minimum (int grades[], int no_of_grades);
        int maximum (int grades[], int no_of_grades);
    
    int main()
    {
    
          int grades[10], i, min_grades = 0; 
          int max_grades = 0;
          int sum = 0;
          int no_of_grades = 0;
          int avg = 0;
    
          do
          {
    
          printf ("Enter the number of grades to process (0-10): ", no_of_grades);
          scanf ("&#37;i", &no_of_grades);
          fflush (stdin);
    
          if (no_of_grades < 1 || no_of_grades > 10)
            printf ("Invalid number of grades.\n");
          } while (no_of_grades < 1 || no_of_grades > 10);
    
    
             for (i = 0; i < no_of_grades; i++ )
    do
          {
            printf ("Enter grade for student #%i: ",i+1);
            scanf ("%i", &grades[i]);
            fflush(stdin);
    
            if (grades[i] <= 0 || grades[i] >= 101)
              printf ("***Invalid grade entered.  Please enter 0 - 100.***");
            sum = sum + grades[i];
             } while (grades[i] <= 0 || grades[i] >= 101);
    
            printf ("\nThe sum of all grades is %i",sum);
            
            avg = sum / no_of_grades;
    
    
            min_grades = minimum(grades, no_of_grades);
            max_grades = maximum(grades, no_of_grades);
    
            printf ("\nThe class average is %i",avg);
            printf ("\nThe minimum grade is %i",min_grades);
            printf ("\nThe maximum grade is %i",max_grades);
    
            getchar(); 
    
         } /*End Main*/
    
     
    
         
    
        int minimum (int grades[], int no_of_grades)
    
    {
    
        int min_grades, i;
    
        min_grades = grades[0];
    
        
    
        for (i = 1; i  < no_of_grades; ++i)
    
          if (grades[i] < min_grades)
    
          min_grades = grades[i];
    
          
    
        return min_grades;
    
    }
     
    
        int maximum (int grades[], int no_of_grades)
    
    {
    
        int max_grades, i;
    
        max_grades = grades[0];
    
        
    
        for (i = 1; i  < no_of_grades; ++i)
    
          if (grades[i] > max_grades)
    
          max_grades = grades[i];
    
          
    
        return max_grades;
    
    }
    Last edited by Salem; 09-16-2007 at 03:55 PM. Reason: Fix positioning of code tags

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your problem is that your code executes the sum = sum + grades[i]; part even when the score is bad. You should print your error message if the score is bad, otherwise add it to the sum.

    BTW, there is a separate C forum that is for questions in the C language.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first, you are using C, so this should be in the other forum.
    this code doesnt look right:
    Code:
            if (grades[i] <= 0 || grades[i] >= 101)
              printf ("***Invalid grade entered.  Please enter 0 - 100.***");
            sum = sum + grades[i];
    you are saying to enter 0-100, however it will only accept > 0 and < 101, so the first part should be < 0 not <=. also, i think your sum statement should be in an 'else' clause. if the grade isnt acceptable, you shouldnt add it to the sum, only when the grade IS acceptable, ie in an else clause.

    now some side notes:
    - notice 'avg' is an integer so it would be more accurate to use some floating-point data type
    - your lack of {}s (block statements) in your for loops is no problem in this program, but i would bet a large sum of money that in the future if you continue to do that you will be pulling your hair out at least once trying to find an error when the problem was that you had more than one statement in your for loop and you forgot to add {}s

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Apart from using fflush(stdin), see the FAQ, and some fairly poor code indentation, what seems to be the problem?

    > When I run the program my "sum" command is adding in the grades that are in error
    OIC, consider using 'else'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM