1 / 20

Quiz2-2

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.

gannon
Download Presentation

Quiz2-2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Quiz2-2 Return your Quiz2-2 To enter scores to D2L

  2. 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.

  3. 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

  4. 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

  5. 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;

  6. // 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

  7. // 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?

  8. // 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 ++; }

  9. // 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; } }

  10. 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

  11. // 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?

  12. // 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.";

  13. 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;

  14. 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;

  15. Example int num; cin >> num; // DO_02: // While num is not positive // Increment its value by 2 while (num <= 0) num += 2;

  16. 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; }

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

  18. 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?

  19. 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!

  20. 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!

More Related