













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Chanchal Mahanthapa delivered this lecture at Chandra Shekhar Azad University of Agriculture and Technology for Programming and Computer Architecture. It includes: Control, Constructs, While, Do-while, Repetition, Structure, Pseudocode, Flowchart, Controlled
Typology: Slides
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Formulating Algorithms (Counter-Controlled Repetition)^ ^ Counter-controlled repetition^ ◦^
Loop repeated until counter reaches certainvalue Definite repetition ◦ Number of repetitions known Example^ A class of ten students took a quiz.The grades(integers in the range 0 to 100) for this quiz areavailable to you. Determine the class average onthe quiz.
Formulating Algorithms (Counter-Controlled Repetition)^ ^ Pseudocode for example:
Set total to zeroSet grade counter to oneWhile grade counter is less than or equal to tenInput the next gradeAdd the grade into the totalAdd one to the grade counterSet the class average to the total divided by tenPrint the class average Next: C++ code for this example
21 // processing phase 22 while ( gradeCounter <= 10 ) {
// loop 10 times
23 cout << "Enter grade: ";
// prompt for input
24 cin >> grade;
// read grade from user
25 total = total + grade;
// add grade to total
26 gradeCounter = gradeCounter + 1; // increment counter 27 } 2829 // termination phase 30 average = total / 10;
// integer division
3132 // display result 33 cout << "Class average is " << average << endl; 3435 return 0;
// indicate program ended successfully 3637 } // end function main Enter^ grade:
Enter^ grade:
Enter^ grade:
Enter^ grade:
Enter^ grade:
Enter^ grade:
Enter^ grade:
Enter^ grade:
Enter^ grade:
Enter^ grade:
Class^ average
is^81
Formulating Algorithms (Sentinel-Controlled Repetition)^ ^ Suppose problem becomes:
Develop a class-averaging program that will processan random number of grades each time the programis run ◦ Unknown number of students ◦ How will program know when to end? Sentinel value ◦ Indicates “end of data entry” ◦ Loop ends when sentinel input ◦ Sentinel chosen so it cannot be confused withregular input^ ^ -1 in this case
1 // code2.cpp 2 // Class average program with sentinel-controlled repetition. 3 #include
// parameterized stream manipulators 1112 using std::setprecision; // sets numeric output precision 1314 // function main begins program execution 15 int main() 16 { 17 int total;
// sum of grades 18 int gradeCounter; // number of grades entered 19 int grade;
// grade value 2021 double average;
// number with decimal point for average 2223 // initialization phase 24 total = 0;
// initialize total 25 gradeCounter = 0; // initialize loop counter
2627 // processing phase 28 // get first grade from user 29 cout << "Enter grade, -1 to end: "; // prompt for input 30 cin >> grade;
// read grade from user
3132 // loop until sentinel value read from user 33 while ( grade != -1 ) { 34 total = total + grade;
// add grade to total
35 gradeCounter = gradeCounter + 1; // increment counter 3637 cout << "Enter grade, -1 to end: "; // prompt for input 38 cin >> grade;
// read next grade
3940 } // end while 4142 // termination phase 43 // if user entered at least one grade ... 44 if ( gradeCounter != 0 ) { 4546 // calculate average of all grades entered 47 average = static_cast
1 // code3.cpp 2 // Analysis of examination results. 3 #include
// number of passes 14 int failures = 0;
// number of failures 15 int studentCounter = 1;
// student counter
16 int result;
// one exam result 1718 // process 10 students using counter-controlled loop 19 while ( studentCounter <= 10 ) { 2021 // prompt user for input and obtain value from user 22 cout << "Enter result (1 = pass, 2 = fail): "; 23 cin >> result; 24
Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Passed 6Failed 4Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Passed 9Failed 1Raise tuition
true false action(s) condition