Instructions:
* Use the steps to create a template program the same as above, but use a different Project_name, for example Program6
* Replace function main program block, with the following:
int counter; / * number of grades ENTERED * /
int grade; / * grade value * /
int total; / * sum of grades * /
float average; / * number with decimal point for average * /
/ * Initialization phase * /
total = 0; / * initialize total * /
counter = 0; / * initialize loop counter * /
/ * Processing phase * /
/ * Get first grade from user * /
printf ("Enter grade, -1 to end:"); / * prompt for input * /
scanf ("% d", & grade); / * read grade from user * /
/ * Loop while not yet sentinel value read from user * /
while (grade! = -1) {
total = total + grade; / * add grade to total * /
counter = counter + 1; / * increment counter * /
/ * Get next grade from user * /
printf ("Enter grade, -1 to end:"); / * prompt for input * /
scanf ("% d", & grade); / * read next grade * /
} / * End while * /
/ * Termination phase * /
/ * If user ENTERED at least 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% .2 f \ n", average);
} / * End if * /
else {/ * if no grades were the resource persons ENTERED, the output message * /
printf ("No grades were the resource persons ENTERED \ n");
} / * End else * /
return 0; / * indicate program ended successfully * /
Program Output:
Enter grade, -1 to end: 75
Enter grade, -1 to end: 94
Enter grade, -1 to end: 97
Enter grade, -1 to end: 88
Enter grade, -1 to end: 70
Enter grade, -1 to end: 64
Enter grade, -1 to end: 83
Enter grade, -1 to end: 89
Enter grade, -1 to end: -1
Class average is 82.50
Enter grade, -1 to end: -1
No grades were the resource persons ENTERED
Enjoy reading the article Structured program with a sentinel loop in the Science Online. Hopefully this article Structured program with a sentinel loop are able to increase knowledge and can be beneficial to you. don't forget to read the article as well other than Structured program with a sentinel loop in this blog
0 comments For Structured program with a sentinel loop Article
What do you think about Structured program with a sentinel loop article??? Please give your comments about Structured program with a sentinel loop article in this Blog. give your comments about Structured program with a sentinel loop article, either in the form of criticism and advice and support will be very meaningful for me to progress this blogPost a Comment