G2A Many GEOs
SHARE
TWEET

Untitled

ThErHiNo Apr 7th, 2020 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //class averge problem with sential-controlled iteration
  2. #include<stdio.h>
  3.  
  4. // function main begihns program execution
  5. int main(void)
  6. {
  7.     unsigned int counter; //number of grades average
  8.     int grade;//grade value
  9.     int total; //sum of grades
  10.    
  11.     float average; // number with decimal point for average
  12.    
  13.     //initialization phase
  14.     toatl = 0 ;//initialize toatal
  15.     counter= 0 ; //initialize loop counter
  16.    
  17.     //processing phase
  18.     //get first grade from user
  19.     printf("%s", Enter grade, -1 to end:");//prompt for input
  20.    scanf("%d", &grade);//read grade from user
  21.    
  22.    //loop whille sentinel value not read yet from user
  23.    while(grade!=1){
  24.        total=total+grade;//add grade to total
  25.        counter= counter + 1 ; //inreament counter
  26.        
  27.        //get next grade from user
  28.        printf("%s","Enter grade, -1 to end");//prompt for input
  29.        scanf("%d",&grade);//increamencounter
  30.        }//end while
  31.        
  32.    //termination phase
  33.    // if user entered atleast one grade
  34.    if(counter!=0){
  35.    //calculate average of all grades entered
  36.        average=(float)total/ counter ;//avoid truncation
  37.        
  38.    //display average with two digits of precision
  39.        printf("Class Average is %.2f\n", average);
  40.        }//end if
  41.     else{//if no grades were entered output meassage
  42.        puts("No grades were entered");}//end else    
  43. }//end main function
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top