1 cs 1430: programming in c++. 2 input: 45.5 55 60.5 39.5 -1 input ends with -1 sentinel-controlled...
TRANSCRIPT
1
CS 1430: Programming in C++
2
Input: 45.5 55 60.5 39.5 -1Input ends with -1Sentinel-Controlled Loop
Input: 4 45.5 55 60.5 39.5 Input begins with 4 (count)Count-Controlled Loop
Count-Controlled Loop
Input: 4 45.5 55 60.5 39.5
Variables totalCount: number of scores to processloopCount: number of iterations the loop body
has been executed (number of scores processed)
Pseudo CodeInput totalCount Set loopCount to 0 // LCVInitializing other variables While loopCount < totalCount Input the score Process score Increase loopCount by 1
3
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;
// The range is knownconst float MIN_SCORE = 0.0; const float MAX_SCORE = 60.0;
float score, max = MIN_SCORE; int totalCount, loopCount;
// Interactive modecout << "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 < MIN_SCORE || score > MAX_SCORE) cout << "Invalid score: " << score; else { if (score > max) max = score; }
loopCount ++; } // Four Parts of Loop
5
// 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 < MIN_SCORE || 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
// The range is NOT known
float score, max; int totalCount, loopCount;
// Batch modecin >> 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
Average and Max of n ScoresRange is known
Invalid scores are not considered
8
Initialize (total, max, validCount)Input totalCountWhile 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
// 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 < MIN_SCORE || 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
// validCount could be 0!
// while loop to compute max, total and validCountwhile (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
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
Example
Input an integerIf it’s positive Add it to total Otherwise Multiply it to total
12
Example
While num is not positive Increment its value by 2
13
Example
Input a num and add it to total until num is not positive
14
Factorial
N! = 1 * 2 * 3 *…* (N - 1) * N0! = 11! = 12! = 1 * 2 = 2…5! = 1 * 2 * 3 * 4 * 5 = 120…
15
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
…
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
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
Schedule
Quiz3-2 Due 5 PM Monday Program 1 Due 9:30 PM Monday Style
19