notes2

Input the first grade possibly the flag value while

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: /* initialize variables */ int counter = 1; float angle; /* process while loop */ while ( counter <= 10 ) { printf( "Enter angle %d in degrees: ", counter ); scanf( "%f", &angle ); printf( "Angle %d in radians is %.2f.\n", counter, angle*3.1416/180); counter = counter + 1; } return 0; } /* end function main */ Sentinel-Controlled Repetition • Sentinel-controlled repetition – Loop repeated until user enters a sentinel value (also called a flag value) – Flag value indicates “end of data entry” – End loop when user enters flag value – Flag value chosen so that it cannot be confused with a regular input (such as -1 in the following example) – Example: A class of students took a quiz. The grades for this quiz are available to you. Determine the class average on the quiz. Sentinel-Controlled Repetition – Pseudocode Set total to zero and counter to zero. Input the first grade (possibly the flag value). While grade not equal to sentinel, Add the grade to the total. Add 1 to counter Input the next grade (possibly the flag value). If counter is not equal to zero, Set average to the total divided by the counter. Print the average. Else Print “No grades were entered.” Ex. Prog. – Sentinel-controlled repetition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /* class average program with sentinel-controlled repetition */ #inclu...
View Full Document

This note was uploaded on 03/02/2009 for the course ENGR 3 taught by Professor Ben-yaakov during the Fall '08 term at UCSB.

Ask a homework question - tutors are online