Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 1430: Programming in C++. 2 Input: 45.5 55 60.5 39.5 -1 Input ends with -1 Sentinel-Controlled Loop Input: 4 45.5 55 60.5 39.5 Input begins with.

Similar presentations


Presentation on theme: "1 CS 1430: Programming in C++. 2 Input: 45.5 55 60.5 39.5 -1 Input ends with -1 Sentinel-Controlled Loop Input: 4 45.5 55 60.5 39.5 Input begins with."— Presentation transcript:

1 1 CS 1430: Programming in C++

2 2 Input: 45.5 55 60.5 39.5 -1 Input ends with -1 Sentinel-Controlled Loop Input: 4 45.5 55 60.5 39.5 Input begins with 4 (count) Count-Controlled Loop

3 Input: 4 45.5 55 60.5 39.5 Variables totalCount: number of scores to process loopCount: number of iterations the loop body has been executed (number of scores processed) Pseudo Code Input totalCount Set loopCount to 0 // LCV Initializing other variables While loopCount < totalCount Input the score Process score Increase loopCount by 1 3

4 How to Initialize? Range is known const float MIN_SCORE = 0.0; const float MAX_SCORE = 60.0; // Initialize max max = MIN_SCORE; // Initialize min min = MAX_SCORE; // Initialize total total = 0; 4 Range is NOT known // Input the first score cin >> score; // Initialize max max = score; // Initialize min min = score; // Initialize total total = score;

5 // The range is known const float MIN_SCORE = 0.0; const float MAX_SCORE = 60.0; float score, max = MIN_SCORE; int totalCount, loopCount; // Interactive mode cout << "Enter the count of scores: "; cin >> totalCount; loopCount = 0; // No prime read of score! while (loopCount < totalCount) { // Interactive mode cout << "Enter a score: "; cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { if (score > max) max = score; } loopCount ++; } // Four Parts of Loop 5

6 // Another way: No loopCount float score, max = MIN_SCORE; int totalCount; // Batch mode: no input prompt // cout << "Enter the count of scores: "; cin >> totalCount; while (totalCount > 0) { // Batch mode: no input prompt // cout << "Enter a score: "; cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { if (score > max) max = score; } totalCount --; // totalCount = totalCount - 1; } // What’s the value of totalCount after the loop? 6

7 // The range is NOT known float score, max; int totalCount, loopCount; // Batch mode cin >> totalCount; loopCount = 0; while (loopCount < totalCount) { // Batch mode cin >> score; if (loopCount == 0) // first score max = score; else { if (score > max) max = score; } loopCount ++; } 7

8 Average and Max of n Scores Range is known Invalid scores are not considered 8 Initialize (total, max, validCount) Input totalCount While totalCount > 0 Input a score If score out of range display a message Else Increase validCount by one add score to total if score > max max = score Decrease totalCount by one

9 // Average and Max of n Scores // Range is known and invalid scores are not considered float score, max = MIN_SCORE, total = 0.0, average; int totalCount, validCount = 0; cin >> totalCount; while (totalCount > 0) { cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { validCount ++; total += score; // total = total + score; if (score > max) max = score; } totalCount --; } average = total / validCount; // Any problems? 9

10 // validCount could be 0! // while loop to compute max, total and validCount while (totalCount > 0) { … } if (validCount > 0) { average = total / validCount; cout << endl << "max = " << max; cout << endl << "average = " << average; // cout << endl << "average = " << total / validCount; } else cout << endl << "No valid scores entered."; 10

11 More Arithmetic Operators validCount ++; // validCount = validCount + 1; totalCount --; // totalCount = totalCount - 1; total += score; // total = total + score; total -= score; // total = total - score; yValue /= xValue; // yValue = yValue / xValue; yValue %= xValue; // yValue = yValue % xValue; yValue *= xValue; // yValue = yValue * xValue; 11

12 Example Input an integer If it’s positive Add it to total Otherwise Multiply it to total 12

13 Example While num is not positive Increment its value by 2 13

14 Example Input a num and add it to total until num is not positive 14

15 Factorial N! = 1 * 2 * 3 *…* (N - 1) * N 0! = 1 1! = 1 2! = 1 * 2 = 2 … 5! = 1 * 2 * 3 * 4 * 5 = 120 … 15

16 Factorial Pseudo Code Input Num (assume Num >= 0) Fact = 1 loopCount = 0 While loopCount < Num loopCount ++ Fact = Fact * loopCount Output Fact 16 N! = 1 * 2 * 3 *…* (N - 1) * N 0! = 1 1! = 1 2! = 1 * 2 = 2 … 5! = 1 * 2 * 3 * 4 * 5 = 120 …

17 Factorial Pseudo Code Input Num Fact = 1 loopCount = Num While loopCount > 1 Fact = Fact * loopCount loopCount -- Output Fact 17 Pseudo Code Input Num Fact = 1 loopCount = Num While loopCount > 1 loopCount -- Fact = Fact * loopCount Output Fact

18 Tracing Nested Loops int xValue, yValue, zValue; xValue = 3; while (xValue > 1) { zValue = 1; yValue = xValue; while (yValue > 0) { zValue *= yValue; yValue --; } cout << "What is this: " << zValue; xValue --; } 18 xValue yValue zValue ? ? ? 3 1 3 3 2 6 1 6 0 2 1 2 2 1 2 0 1

19 Schedule  Quiz3-2 Due 5 PM Monday  Program 1 Due 9:30 PM Monday  Style Style 19


Download ppt "1 CS 1430: Programming in C++. 2 Input: 45.5 55 60.5 39.5 -1 Input ends with -1 Sentinel-Controlled Loop Input: 4 45.5 55 60.5 39.5 Input begins with."

Similar presentations


Ads by Google