200 likes | 286 Views
Quiz2-2. Return your Quiz2-2 To enter scores to D2L. Lab 1. If no differences by 5 pm Thursday Five (5) points Else if no differences by 5 pm Wednesday Three (3) points Else No Points! Same for other Labs Unless specified otherwise.
E N D
Quiz2-2 Return your Quiz2-2 To enter scores to D2L
Lab 1 If no differences by 5 pm Thursday Five (5) points Else if no differences by 5 pm Wednesday Three (3) points Else No Points! Same for other Labs Unless specified otherwise.
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 Find Max of n Scores
Count-Controlled Loop 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
How to Initialize? Range is NOT known // Input the first score cin >> score; // Initialize max max = score; // Initialize min min = score; // Initialize total total = score; 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;
// 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 < MIN_SCORE || score > MAX_SCORE) cout << "Invalid score: " << score; else { if (score > max) max = score; } loopCount ++; } // Four Parts of Loop
// 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?
// 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 ++; }
// The range is NOT known float score, max; inttotalCount, loopCount; // Batch mode cin >> totalCount; loopCount = 0; while (loopCount < totalCount) { loopCount ++; // Batch mode cin >> score; if (loopCount == 1) // first score max = score; else { if (score > max) max = score; } }
Average and Max of n Scores Range is known Invalid scores are not considered 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 Compute average using validCountand total
// Average and Max of n Scores // Range is known and invalid scores are not considered float score, max = MIN_SCORE, total = 0.0, average; inttotalCount, 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?
// 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.";
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;
Example float num, total = 1; // DO_01: // Input a num // If it’s positive // Add it to total // Otherwise // Multiply it to total cin >> num; if (num > 0) total += num; else total *= num;
Example int num; cin >> num; // DO_02: // While num is not positive // Increment its value by 2 while (num <= 0) num += 2;
Example float num, total; cin >> total; // DO_03: // If total is negative // Input a value to num and add it to total // until total is not negative while (total < 0) { cin >> num; total += num; }
Factorial 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 (assume Num >= 0) Fact = 1 loopCount = 0 While loopCount < Num loopCount ++ Fact = Fact * loopCount Output Fact N! = 1 * 2 * 3 *…* (N - 1) * N 0! = 1 1! = 1 2! = 1 * 2 = 2 … 5! = 1 * 2 * 3 * 4 * 5 = 120 … How to compute n! for any n?
Factorial Pseudo Code Input Num Fact = 1 loopCount = Num While loopCount > 1 loopCount -- Fact = Fact * loopCount Output Fact Correct? NO! Pseudo Code Input Num Fact = 1 loopCount = Num While loopCount > 1 Fact = Fact * loopCount loopCount -- Output Fact Correct!
Schedule • Program 1 Drop your *.cpp file by Monday to S:\Courses\CSSE\yangq\CS1430 • Quiz3-3 Due 5 PM Monday • Quiz3-2 Now One Bonus Point!