Sentinel-Controlled repetition

This is a discussion on Sentinel-Controlled repetition within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main( void ) { int grade; int total; int counter; float average; total = 0; counter ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Sentinel-Controlled repetition

    Code:
    #include <stdio.h>
    
    int main( void )
    
    {
       int grade;
       int total;
       int counter;
       float average;
    
       total = 0;
       counter = 0;
    
       printf ("Enter grade, -1 to find class average\n");
       scanf ("&#37;d", &grade);
    
       while (grade != -1)
       {
          total = total + grade;
          counter = counter + 1;
          average = (float) total / counter;
    
          printf ("Enter grade, -1 to find class average\n");
          scanf ("%d", &grade);
       }
    
       if (average <= 74)
       {
          printf ("The average grade is %.2f\n", average);
          printf ("0-74: F\n");
          printf ("Enter grade, -1 to find class average, -2 to quit\n");
          scanff ("%d", &grade);
       }
          
       else
          if (average <= 82)
          {
          printf ("The average grade is %.2f\n", average);
          printf ("75-82: C\n");
          printf ("Enter grade, -1 to find class average, -2 to quit\n");
          scanff ("%d", &grade);
          }
    
          else
             if (average <= 91)
             {
                printf ("The average grade is %.2f\n", average);
                printf ("83-91: B\n");
                printf ("Enter grade, -1 to find class average, -2 to quit\n");
                scanff ("%d", &grade);         
             }
       
       else
          if (average <= 100)
          {
             printf ("The average grade is %.2f\n", average);
             printf ("92-100: A\n");
             printf ("Enter grade, -1 to find class average, -2 to quit\n");
             scanff ("%d", &grade);         
          }
    
          return 0;
    }
    2 Problems I am having here.

    1. The loop does not continue after typing "-1" to find the grade average. I tried inserting another "while" statement after the "if...else" statements, but it just terminates after I enter "-1" again without printing the grade average.

    2. Not quite sure how to get the program to terminate on my terms. Its terminating itself because it's not running back through the loop after typing "-1", but once thats fixed I'm not sure how to go about making it terminate using the sentinal value. I tried a "while" statement using "-2" but my printf was not displayed and the program just quit for the previously mentioned reason.

    Any help would be appreciated both on those issues and my coding in general. I want to establish good habits from the start. If at all possible, it would be great to be pointed in the right direction without actually being given the exact answer. Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want something to happen again, then you have to put it inside a loop. So whatever statements you want to repeat for another student, or whatever, you need to put inside a while loop guarded by -2.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Quote Originally Posted by tabstop View Post
    If you want something to happen again, then you have to put it inside a loop. So whatever statements you want to repeat for another student, or whatever, you need to put inside a while loop guarded by -2.
    Ok, I understand what you mean about guarding -2, same way as -1 is guarded, but I still can't get the program to loop back around after I input the first -1. I have a feeling it has to do with my if conditions. After each "if" I have a scanf for input, but the program doesn't know to restart the process all over again. I have to get the ironed out before I worry about -2 terminating the program.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, -1 breaks out of (what will become your inner, but is now your only) loop. Then you do the if-elseif-elseif bit. Then, since you haven't written an outer loop statement yet, it stops, because that's the bottom of the program.

    You want everything between total = 0; and the close curly before return to repeat, yes? So put it all in a loop. It will not repeat just because; it will only repeat if you tell it to do so.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Hybrid View Post
    Code:
    #include <stdio.h>
    
    int main( void )
    
    {
       int grade;
       int total;
       int counter;
       float average;
    
       total = 0;
       counter = 0;
    
       printf ("Enter grade, -1 to find class average\n");
       scanf ("%d", &grade);
    
       while (grade != -1)
       {
          total = total + grade;
          counter = counter + 1;
          average = (float) total / counter;
    
    2      printf ("Enter grade, -1 to find class average\n");
          scanf ("%d", &grade);
       }
    
       if (average <= 74)
       {
          printf ("The average grade is %.2f\n", average);
          printf ("0-74: F\n");
          printf ("Enter grade, -1 to find class average, -2 to quit\n");
          scanff ("%d", &grade);
       }
          
       else
          if (average <= 82)
          {
          printf ("The average grade is %.2f\n", average);
          printf ("75-82: C\n");
          printf ("Enter grade, -1 to find class average, -2 to quit\n");
          scanff ("%d", &grade);
          }
    
          else
             if (average <= 91)
             {
                printf ("The average grade is %.2f\n", average);
                printf ("83-91: B\n");
                printf ("Enter grade, -1 to find class average, -2 to quit\n");
                scanff ("%d", &grade);         
             }
       
       else
          if (average <= 100)
          {
             printf ("The average grade is %.2f\n", average);
             printf ("92-100: A\n");
             printf ("Enter grade, -1 to find class average, -2 to quit\n");
             scanff ("%d", &grade);         
          }
    
          return 0;
    }
    2 Problems I am having here.

    1. The loop does not continue after typing "-1" to find the grade average. I tried inserting another "while" statement after the "if...else" statements, but it just terminates after I enter "-1" again without printing the grade average.

    2. Not quite sure how to get the program to terminate on my terms. Its terminating itself because it's not running back through the loop after typing "-1", but once thats fixed I'm not sure how to go about making it terminate using the sentinal value. I tried a "while" statement using "-2" but my printf was not displayed and the program just quit for the previously mentioned reason.

    Any help would be appreciated both on those issues and my coding in general. I want to establish good habits from the start. If at all possible, it would be great to be pointed in the right direction without actually being given the exact answer. Thanks!
    When you see yourself repeating the same code, over and over, you don't have to worry about your loop for it - you have to think that it's just a miserable design for your program.

    You've worked too hard, and overthought this whole program. The code in red, above is trash - throw it away, please.

    Please tell me about the grade average numbers. What do you want to do with them, or what are they for? We'll get this right.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    
    int main( void )
    
    {
       int grade;
       int total;
       int counter;
       float average;
    
       total = 0;
       counter = 0;
       /* up here, it's nice to print up the name of the program and version number, author's name, and what the program is going to do */
    
       do
       {
    
          printf ("Enter grade,  or -1 to find class average\n");
          scanf ("&#37;d", &grade);
    
          if(grade != -1)
          {
             total = total + grade;
             counter = counter + 1;
             average = (float) total / counter;
          }
       }while (grade != -1);
    
    
    /* in here we'll do something with those average grade numbers you had in the first version
    of the program */
    
    
       return 0;
    }
    Something closer.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Quote Originally Posted by Adak View Post
    Code:
    #include <stdio.h>
    
    int main( void )
    
    {
       int grade;
       int total;
       int counter;
       float average;
    
       total = 0;
       counter = 0;
       /* up here, it's nice to print up the name of the program and version number, author's name, and what the program is going to do */
    
       do
       {
    
          printf ("Enter grade,  or -1 to find class average\n");
          scanf ("%d", &grade);
    
          if(grade != -1)
          {
             total = total + grade;
             counter = counter + 1;
             average = (float) total / counter;
          }
       }while (grade != -1);
    
    
    /* in here we'll do something with those average grade numbers you had in the first version
    of the program */
    
    
       return 0;
    }
    Something closer.
    I had a feeling all those "if...else" weren't really needed, but I was trying to do this from memory and not from the book. That was the only way I could think to do it.

    Basically I need a program that will allow grades to be entered, totaled, averaged, and then output that average as well as the corresponding grade. Then I need it to start over unless it's told to do otherwise.

    I think I was getting on the right track after Tab told me about the inner and outer loops, although I still wasn't able to get the program to start back from scratch. I'm not sure what statement I need to use to have the program repeat the repetition statement.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Hybrid View Post
    I had a feeling all those "if...else" weren't really needed, but I was trying to do this from memory and not from the book. That was the only way I could think to do it.

    Basically I need a program that will allow grades to be entered, totaled, averaged, and then output that average as well as the corresponding grade. Then I need it to start over unless it's told to do otherwise.

    I think I was getting on the right track after Tab told me about the inner and outer loops, although I still wasn't able to get the program to start back from scratch. I'm not sure what statement I need to use to have the program repeat the repetition statement.
    If something is needed for output for each individual grade, then we need the logic for that, inside the do while loop:

    Code:
     do
       {
    
          printf ("Enter grade,  or -1 to find class average\n");
          scanf ("%d", &grade);
    
          if(grade != -1)
          {
             total = total + grade;
             counter = counter + 1;
             average = (float) total / counter;
    
             if(grade < 60)
                 printf("\nThat is an F");
             else if(grade >= 60 && grade < 70)
                 printf("\nThat is a D");
              else if(grade >= 70 && grade < 80)
                 printf("\nThat is a C");
              /* etc. */          
          }
       }while (grade != -1);
    
    /* out here we print up anything about the average grade for the class */
    One set of those if statements is needed, but not one set for every grade.

    All loops are similar in C

    Do While are ALWAYS entered into FIRST, then the loop test is made at the bottom of the loop.

    While loops (while is at the top of the loop), are entered in only when the condition controlling the loop, is met. The loop may or may not be entered into.

    For loops generally are used when you know how many times you want the loop to run through. They can be used anywhere either of the above loops are used, although not always as elegantly.

    All loops can be nested:

    Code:
     
    for (row = 0; row < maxrows; row++)
       for (col = 0; col < maxcols; col++)
           printf("%d", array[row][col]);
    Code here will print out a table or two dimension array, using nested for loops. This is a very standard bit of code in C.

    Hope that helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sentinel controlled loop
    By arjay in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 04:30 AM
  2. sentinel controlled repetition example
    By droseman in forum C Programming
    Replies: 7
    Last Post: 09-26-2008, 02:17 AM
  3. Problem with sentinel values
    By Golfduffer in forum C Programming
    Replies: 3
    Last Post: 08-30-2007, 01:19 AM
  4. rb tree Sentinel NIL
    By dinjas in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 11:28 PM
  5. Sentinel while loop and errors.
    By lollypop in forum C Programming
    Replies: 12
    Last Post: 10-19-2003, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21