Presentation is loading. Please wait.

Presentation is loading. Please wait.

Controlling Function Behavior Sequence, Selection and Repetition.

Similar presentations


Presentation on theme: "Controlling Function Behavior Sequence, Selection and Repetition."— Presentation transcript:

1 Controlling Function Behavior Sequence, Selection and Repetition

2 Review We’ve seen that we can write a simple function that encodes a formula: double EllipseArea(double length, double width); { double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth; } The statements in such a function are executed in sequence (one after another), which is called sequential execution.

3 Difficulty There are other operations that are difficult to implement, using just sequential execution. Example: Let’s write a program to read in a sequence of test scores, and display their average and a corresponding pass/fail grade.

4 Behavior Our program should prompt for, read, and average a sequence of test scores. It should then display the average. It should then compute the pass-fail letter grade (P or F) corresponding to that average. Finally, it should display that letter grade.

5 Objects Description Type Kind Name average double varying average P/F grade char varying grade sequence of ??? ??? ??? test scores

6 Operations Description Predefined? Library? Name prompt for, read no -- -- and average a sequence of doubles display a double yes iostream << compute grade no -- -- display char yes iostream <<

7 Algorithm 0. Display via cout the purpose of the program. 1. Compute average by prompting for, reading and averaging a sequence of test scores. 2. Display average. 3. Compute P/F letter grade corresponding to average. 4. Display grade.

8 Discussion We have predefined operations for steps 0, 2 and 4 of our algorithm, but no predefined operations for steps 1 or 3... Solution: Build functions to perform those operations.

9 Function 1 Behavior Our function should read, sum, and count the sequence of scores using the input-loop pattern. If the number of scores was positive, it should return sum/numScores. Otherwise (numScores is not positive) it should display an error message and terminate the program.

10 Function 1 Objects Description Type Kind Name sum of scores double varying sum a score double varying score number of int varying numScores scores a prompt string constant -- error msg string constant --

11 Function 1 Operations Description Predefined? Library? Name test for sentinel yes built-in < display a string yes iostream << read a double yes iostream >> add score to sum yes iostream += repeat above steps yes built-in for select steps yes built-in if increment count yes iostream ++ divide doubles yes built-in / return a double yes built-in return terminate program yes cstdlib exit()

12 Function 1 Algorithm 0. Initialize sum to 0, count to 0. 1. Loop: a. Read score. a. Read score. b. If score < 0: terminate repetition. b. If score < 0: terminate repetition. c. Increment count. c. Increment count. d. Add score to sum. d. Add score to sum. End loop. End loop. 2. If count > 0 Return sum / count. Return sum / count. Else Else Display an error message and terminate the program. Display an error message and terminate the program.

13 Function 1 Coding #include // exit() double ReadAndAverage() { double score, sum = 0.0; int count = 0; for (;;) { cout << “Enter a test score (-1 to quit): “; cin >> score; if (score < 0) break; // test for sentinel count++; sum += score; } if (count > 0) return sum / count; else { cerr << “\n* no scores to average!\n” << endl; exit(1); }

14 Function 2 Behavior Our function should receive from its caller an average. If that average is less than 60, it should return a failing grade; otherwise, it should return a passing grade.

15 Function 2 Objects Description Type Kind Name failing grade char constant ‘F’ average double varying avg passing grade char constant ‘A’

16 Function 2 Operations Description Predefined? Library? Name receive double yes built-in -- return a char yes built-in return select from among yes built-in if different returns

17 Function 2 Algorithm 0. Receive avg from caller. 1. If avg < 60: Return ‘F’. Else Return ‘P’.

18 Function 2 Coding char PassFailGrade(double avg) { if (avg < 60) return ‘F’; else return ‘P’; }

19 Discussion ReadAndAverage() and PassFailGrade() are sufficiently useful that it is probably worth storing them in a library (e.g., grading). Once we have functions for each operation that is not predefined, we can encode the algorithm that solves our problem.

20 Coding #include #include “grading.h” // ReadAndAverage(), PassFailGrade() int main() { cout << “\nThis program reads a sequence of test scores” << “\n and computes their pass-fail grade.\n”; double average = ReadAndAverage(); char grade = PassFailGrade(average); cout << “\nThe average is “ << average << “\n and the grade is “ << grade << endl; }

21 Testing This program reads a sequence of test scores and computes their pass-fail grade. Enter a score: 65 Enter a score: 55 Enter a score: 75 Enter a score: -1 The average is 65 and the grade is P.

22 Summary (i) The C++ for loop lets you repeat a block of statements a specified number of times. Patterns: for (int count = Start; count <= Stop; count++) Statements for (;;) { StatementList 1 if (Condition) break; StatementList 2 } These permit repetitive execution of statements.

23 Summary (ii) The C++ if statement lets you select from among two statements. Pattern: if (BooleanExpression) Statement1 [else Statement2] This permits selective execution of statements.


Download ppt "Controlling Function Behavior Sequence, Selection and Repetition."

Similar presentations


Ads by Google