Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //class averge problem with sential-controlled iteration
- #include<stdio.h>
- // function main begihns program execution
- int main(void)
- {
- unsigned int counter; //number of grades average
- int grade;//grade value
- int total; //sum of grades
- float average; // number with decimal point for average
- //initialization phase
- toatl = 0 ;//initialize toatal
- counter= 0 ; //initialize loop counter
- //processing phase
- //get first grade from user
- printf("%s", Enter grade, -1 to end:");//prompt for input
- scanf("%d", &grade);//read grade from user
- //loop whille sentinel value not read yet from user
- while(grade!=1){
- total=total+grade;//add grade to tota l
- counter= counter + 1 ; //inreament counter
- //get next grade from user
- printf("%s","Enter grade, -1 to end");//prompt for input
- scanf("%d",&grade);//increamencounter
- }//end while
- //termination phase
- // if user entered atleast one grade
- if(counter!=0){
- //calculate average of all grades entered
- average=(float)total/ counter ;//avoid truncation
- //display average with two digits of precision
- printf("Class Average is %.2f\n", average);
- }//end if
- else{//if no grades were entered output meassage
- puts("No grades were entered");}//end else
- }//end main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement