Thread: ?? with trying to sum numbers

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

    ?? with trying to sum numbers

    Help, I can't get this program to work correctly. If I put in a number higher or lower than is allowed it gives me the correct error message. However, when I sum up the numbers it is adding them in the total. How do I get the code to not add in the invalid numbers.
    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 ("%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;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    before others take time to answer, notice it has been solved in the C++ forum. i think Babs21 made a simple mistake of first putting it in the C++ forum.
    see here: http://cboard.cprogramming.com/showthread.php?t=93646

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Help with programming assignment, please!
    By xMEGANx in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2008, 05:29 PM
  3. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 02:47 PM
  4. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM