LectureEight - Lecture 8 Control Structure I(Repetition –...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: Lecture 8 Control Structure I (Repetition) – Part II By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department ٢ Formulating Algorithms (Counter- Controlled Repetition) ¡ Counter-controlled repetition ¡ Loop repeated until counter reaches a certain value. ¡ Definite repetition ¡ Number of repetitions is known ¡ Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. ٣ Formulating Algorithms (Counter- Controlled Repetition) ¡ Pseudocode for example: Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average ¡ Following is the C++ code for this example ٤ 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled repetition 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main() { 1 int total, // sum of grades 2 gradeCounter, // number of grades entered 3 grade, // one grade 4 average; // average of grades 5 6 // initialization phase 7 total = 0; // clear total 8 gradeCounter = 1; // prepare to loop 9 // processing phase 1 while ( gradeCounter <= 10 ) { // loop 10 times 2 cout << "Enter grade: "; // prompt for input 3 cin >> grade; // input grade 4 total = total + grade; // add grade to total 5 gradeCounter = gradeCounter + 1; // increment counter 6 } 7 8 // termination phase 9 average = total / 10; // integer division cout << "Class average is " << average << endl; 1 2 return 0; // indicate program ended successfully 3 } The counter gets incremented each time the loop executes. Eventually, the counter causes the loop to end. ٥ nter grade: 98 nter grade: 76 nter grade: 71 nter grade: 87 nter grade: 83 nter grade: 90 nter grade: 57 nter grade: 79 nter grade: 82 nter grade: 94 lass average is 81 ٦ Formulating Algorithms with Top-Down, Stepwise Refinement (Sentinel-Controlled Repetition) I ¡ Suppose the problem becomes:...
View Full Document

Page1 / 17

LectureEight - Lecture 8 Control Structure I(Repetition –...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online