ads
YouNg Engineer Structured program with a sentinel loop Monday, February 28, 2011 Instructions: * Use the steps to create a template program the same as above, but use a different Project_name, for example Program6 * Re... 5

Structured program with a sentinel loop

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


copy right by
Berbagi Tips dan Trik

Related Posts On

No comments:

Post a Comment


Copyright © Just - onLy

Sponsored By: Free For Download Template By: Fast Loading Seo Friendly Blogger Template