Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.

Similar presentations


Presentation on theme: "1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11."— Presentation transcript:

1 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11

2 2 Announcements Homework 3 –Due Wednesday, 9/28/05 –Page 222-228 –4.1, 4.2, 4.3, 4.10, 4.11, 4.12, 4.33, 4.37, 4.39, 4.47

3 3 Review Control Construct –Conditional or selection constructs

4 4 Outline Switch

5 5 Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice –if-else-if statement if-else statements “glued” together –Switch statement An advanced construct

6 6 A sequence of if statements if (die == 1) cout << “1” << endl; else if (die == 2) cout “2” << endl; else if (die == 3) cout “3” << endl; else if (die == 4) cout “4” << endl; else if (die == 5) cout “5” << endl; else cout “6” << endl; cout << “Roll again, sucker!” << endl;

7 7 A switch statement switch (die) { case 1: cout << “1” << endl; break; case 2: cout << “2” << endl; break; case 3:cout << “3” << endl; break; case 4:cout << “4” << endl; break; case 5:cout << “5” << endl; break; default: cout << “6” << endl; } Which code would you rather deal with?? What if I wanted to allow for the roll for two dice?

8 8 A switch statement More than one statement is allowed in a case, without turning it into a block –The statements after the corresponding case are executed until A break statement occurs The end of the switch block

9 9 A switch statement switch (die) { case 1: cout << “1”; cout << “ is a lot of fun”; break; case 2: cout << “2”; cout << “ now that’s cool”; break; case 3: cout << “3”; break; case 4: cout << “4”; break; case 5: cout << “5”; break; default: cout << “6”; } cout << endl;

10 10 A switch statement switch (die) { case 1: case 2: case 3: cout << “die between 1 and 3”; case 4: case 5: case 6: cout << “die between 1 and 6”; } cout << endl;

11 11 A switch statement Notice that is a case is satisfied and does not have a corresponding break, all the following cases are evaluated The default case is not required

12 12 A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

13 13 cout << "Enter simple expression: "; int left; int right; char operator; cin >> left >> operator >> right; cout << left << " " << operator << " " << right << " = "; switch (operator) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }

14 14 The while Repetition Structure Repetition structure –Programmer specifies an action to be repeated while some condition remains true –Psuedocode while there are more items on my shopping list Purchase next item and cross it off my list –Body of the while loop is repeated until condition becomes false

15 15 The while Repetition Structure Example int product = 2; while ( product <= 1000 ) product = 2 * product;

16 16 The while Repetition Structure Expression Action true false

17 17 Repetition Example Counter-controlled repetition –Loop repeated until counter reaches a certain value. Definite repetition –Known number of repetitions 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.

18 18 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 Repetition Example Pseudocode

19 19 #include using namespace std; int main() { int total = 0; // sum of the grades int grade; // individual grade int gradeCnt = 1; // number of grades entered while (gradeCnt <= 10) { cout << “Enter grade: ” << gradeCnt << “ ”; cin >> grade; total += grade; gradeCnt++; } // end while(gradeCnt <= 10) cout << “Class average is: “ << total / gradeCnt; cout << endl; return 0; } // end main() Repetition Example

20 20 Enter grade: 1 98 Enter grade: 2 76 Enter grade: 3 71 Enter grade: 4 87 Enter grade: 5 83 Enter grade: 6 90 Enter grade: 7 57 Enter grade: 8 79 Enter grade: 9 82 Enter grade: 10 94 Class average is: 81 Repetition Example

21 21 Sentinel-Controlled Repetition Suppose the problem becomes: Develop a class-averaging program that will process an arbitrary number of grades each time the program is run. –Unknown number of students How will the program know to end?

22 22 Sentinel-Controlled Repetition Sentinel value –Indicates “end of data entry” –Loop ends when sentinel inputted –Sentinel value selected so it cannot be confused with a regular input Such as -1 in this case

23 23 Sentinel-Controlled Repetition Top-down, stepwise refinement –Begin with a pseudocode representation of the top: Determine the class average for the quiz –Divide top into smaller tasks and list them in order: Define and initialize variables Input, sum and count the quiz grades Calculate and print the class average

24 24 Sentinel-Controlled Repetition Many programs can be divided into three phases: –Definition and initialization Define and initialize program variables –Processing Inputs data values Adjusts program variables –Termination Calculates and prints the final results

25 25 Sentinel-Controlled Repetition Initialize total to zero Initialize counter to zero Input the first grade (possibly the sentinel) While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel) If the counter is not equal to zero Set the average to the total divided by the counter Print the average Else Print “No grades were entered” Pseudocode solution

26 26 #include using namespace std; int main() { int total = 0; // sum of the grades int grade; // individual grade int gradeCnt = 0; // number of grades entered cout << “Enter grade, -1 to quit “; cin >> grade; while (grade != -1) { total += grade; gradeCnt++; cout << “Enter grade, -1 to quit “; cin >> grade; } // end while(grade != -1) Repetition Example

27 27 if (gradeCnt != 0) { cout << “Class average: “ << total / gradeCnt; cout << endl; } else { cout << “No grades to average, duh!” << endl; } return 0; } // end main() Repetition Example


Download ppt "1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11."

Similar presentations


Ads by Google