0

I have a homework problem in which I have to record grades, put them in an array, and give the average. The program is supposed to ask for input until a negative number is given as a grade OR the array is filled. So far I am able to make the program loop until I give a negative number, and it calculates the correct average. What I can't figure out is how to terminate the loop when the array is filled. My code:

#include <stdio.h>

/* function main begins program execution */
int main( void )
{  int counter; /* number of grade to be entered next */
   int grade; /* grade value */
   int total; /* sum of grades input by user */
   double average; /* average of grades */

   /* initialization phase */
   total = 0; /* initialize total */
   counter = 0; /* initialize counter */
   grade = 0;  /* initialize grade */

   printf( "Input a negative number when done entering grades.\n" );

   /* processing phase */
   #define MAX_GRADES 20
   int grades [MAX_GRADES];
   while ( counter < MAX_GRADES) {
     while ( grade >= 0 ) { /* loop until negative given */
       printf( "Enter grade: " ); /* prompt for input */
       scanf( "%d", &grade ); /* read grade from user */
       if (grade >= 0) {
     if (grade > 100)
       printf( "Grade is greater than 100. Please input grade again.\n" );
     else {
       grades[counter] = grade;
       total = total + grade; /* add grade to total */
       counter = counter + 1;
     } /* end else */
       } /* end if */
     } /* end while */
   } /* end while */

   /* termination phase */
    average = total /(double) counter; /* integer division */

   printf( "Class average is %f\n", average ); /* display result */
   return 0; /* indicate program ended successfully */
} /* end function main */
5
  • Instead of those horrible /* end else */ etc. comments, you should perhaps indent your code in a way that makes sense. Then it will be obvious which bracket closes which one.
    – user529758
    Jan 18, 2013 at 19:33
  • As a side note, kudos for saying "I have a homework problem". Surprisingly many people posting homework problems don't say that.
    – jimwise
    Jan 18, 2013 at 19:36
  • Joe, when you post code here you should use 4 spaces instead of tabs. The indentation issues that @H2CO3 is talking about is due to your use of tabs.
    – Mike
    Jan 18, 2013 at 19:36
  • Okay I'll keep that in mind. I pretty much copied the formatting from my homework, so I'll replace the tabs next time. Thanks for the tip!
    – Joe Hill
    Jan 18, 2013 at 19:41
  • IMHO it's not that tabs are evil per se, rather HTML can't handle them.
    – Agent_L
    Jan 18, 2013 at 19:44

3 Answers 3

1
while ( counter < MAX_GRADES) {
    while ( grade >= 0 ) {

A loop within a loop will iterate x*y times. For each step of outer loop, the inner one will run from beginning to an end.

you need one loop checking both conditions:

while ( counter < MAX_GRADES && grade >= 0) 

However, because you want to FIRST do things and THEN check for condition, the do..while loop suits here better. You can also break out of one loop at any time or continue to finish current run and move to next:

do{ /* loop */
   printf( "Enter grade: " ); /* prompt for input */
   scanf( "%d", &grade ); /* read grade from user */
   if (grade < 0)
     break;

   if (grade > 100){
     printf( "Grade is greater than 100. Please input grade again.\n" );
     continue;  
   }

   /* all abnormal conditions have been handled */
   /* now we're clear to do the actual job */

   grades[counter] = grade;
   total = total + grade; /* add grade to total */
   counter = counter + 1;

 }while ( counter < MAX_GRADES ) /*until array is full*/
4
  • Thanks! Worked like a charm.
    – Joe Hill
    Jan 18, 2013 at 19:35
  • @JoeHill I've added break and continue which I believe are the point of your assignment.
    – Agent_L
    Jan 18, 2013 at 19:37
  • My professor said that he didn't want us using break and continue for this assignment. I'm not sure why, but we'll probably use it at a later date.
    – Joe Hill
    Jan 18, 2013 at 19:44
  • that makes sense, not everything at one time.
    – Agent_L
    Jan 18, 2013 at 19:46
0

Just check if

MAX_GRADES == counter 

and break the loop. But this is done anyway...you don't have to change your code at all.

0

Can you get rid of the inner loop and just change the while condition on the outer loop to:

while ( counter < MAX_GRADES && grade >= 0 )
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.