1 / 23

Quiz2-2

Quiz2-2. // Declare float variables price and cost, and integer variable quantity float price, cost;  int quantity; // Input price with an input PROMPT cout << “Enter the price: ”;  cin >> price;  // Input quantity with an input PROMPT cout << “Enter the quantity: ”;  cin >> quantity; 

yardan
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 // Declare float variables price and cost, and integer variable quantity float price, cost;  int quantity; // Input price with an input PROMPT cout << “Enter the price: ”;  cin >> price;  // Input quantity with an input PROMPT cout << “Enter the quantity: ”;  cin >> quantity;  // If price is positive and quantity is not negative, // Compute cost, which is the product of price and quantity // Display cost at the beginning of next line with message “The cost is ” // Otherwise, display message “Invalid input!” if (price > 0 && quantity >= 0) { cost = price * quantity;  cout << endl << The cost is ” << cost; } else cout << endl << “Invalid input!”;

  2. Sentinel-Controlled Loop Finding MAX of n numbers //const int END_VALUE = -1; -1 is not a magic number! const int LOW_LIMIT = 0; const int HIGH_LIMIT = 60; int score, max; max = LOW_LIMIT; // Range is known cin >> score; // Prime read while (score != END_VALUE) { if (score >= LOW_LIMIT && score <= HIGH_LIMIT) { if (score > max) max = score; } else cout << “Invalid score!”; cin >> score; }

  3. Loop Control Variable (LCV) while (score != -1) { … } • LCV: score • When to exit a loop • Avoid infinite loops

  4. Four Parts of Loops • Loop Initialization (LCV and others) • Testing LCV • Loop Body • Updating LCV

  5. While Loop Initialization (LCV and others) while (LogicalExpression) (Testing LCV) false true Do Work Update LCV Statement after loop

  6. Four Parts of Loops max = LOW_LIMIT; // Range is known cin >> score; // Prime read while (score != -1) { if (score >= LOW_LIMIT && score <= HIGH_LIMIT) { if (score > max) max = score; } else cout << “Invalid score!”; cin >> score; }

  7. Computing Count Range is not Known int score, max, count; cin >> score; // Prime Read while (score != -1) { count ++; // count = count + 1; if (count == 1) // first score max = score; else { if (score > max) max = score; } cin >> score; } if (count == 0) cout << “No scores!”; else cout << “The maximal score is ” << max; // Does it run? // Run time error! // Uninitialized value: count!

  8. Computing Count Range is not Known int score, max, count; count = 0; cin >> score; while (score != -1) { count ++; if (count == 1) max = score; else { if (score > max) max = score; } cin >> score; } if (count == 0) cout << “No scores!”; else cout << “The maximal score is ” << max; // Works even there is no score

  9. Trace in HiC HiCTrace.cpp HiCTraceII.cpp Window/Console View/Status Window Line/Column number Break Point Step Over Reset

  10. Trace Execution Range unknown int score, max, count; count = 0; cin >> score; while (score != -1) { count ++; if (count == 1) max = score; else { if (score > max) max = score; } cin >> score; } if (count == 0) cout << “No scores!”; else cout << “The maximal score is ” << max; Input Scores: 45 55 39 -1 score max count ? ? ? 0 45 1 45 55 2 55 39 3 -1

  11. Tracing Tracing Input: 48 54 66 53 59 -1 score max ? 0 48 48 54 54 66 53 59 59 -1 How to do it? One statement each line Do not cross old values Blank when no new value WILL BE ON QUIZ And Test 1 And Final! Range Known const int LOW_LIMIT = 0; const int HIGH_LIMIT = 60; int score, max = LOW_LIMIT; cin >> score; while (score != -1) { if (score < LOW_LIMIT || score > HIGH_LIMIT) cout << “Invalid score: ” << score; else { if (score > max) max = score; } cin >> score; }

  12. Checking Input Range max = 0; cin >> score; while (score != -1) { if (score >= 0 && score <= 60) { if (score > max) max = score; else cout << “Invalid score!”; } cin >> score; } //else is paired with the last if! max = 0; cin >> score; while (score != -1) { if (score >= 0 && score <= 60) if (score > max) max = score; else cout << “Invalid score!”; cin >> score; } // Is it correct? // Which if is else paired with?

  13. Checking Input Range const int LOW_LIMIT = 0; const int HIGH_LIMIT = 60; max = LOW_LIMIT; cin >> score; // Prime read while (score != -1) { if (score >= LOW_LIMIT && score <= HIGH_LIMIT) { if (score > max) max = score; } else cout << “Invalid score!”; cin >> score; }

  14. Getting Valid Values Pseudo Code Input a value While the input value is out of range Display a message Input a new value cin >> score; while (score < 0 || score > 60) cout << “Invalid score!”; cin >> score; Correct?

  15. Getting Valid Values Pseudo Code Input a value While the input value is out of range Display a message Input a value cin >> score; while (score < 0 || score > 60) { cout << “Invalid score!”; cin >> score; } How about the end value -1?

  16. Getting Valid Values Pseudo Code Input a value While the input value is out of range and not -1 Display a message Input a value cin >> score; while ((score < 0 || score > 60) && score != -1) { cout << “Invalid score!”; cin >> score; } // Can we have while (score < 0 || score > 60 && score != -1) // NO! It’s the same as the following: while (score < 0 || (score > 60 && score != -1))

  17. Getting Valid Scores int score, max = LOW_LIMIT; cout << “Enter a score: ”; cin >> score; while (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)) { cout << “Invalid score: ” << score; cout << “Enter a score: ”; cin >> score; } while (score != END_VALUE) { if (score > max) max = score; cout << “Enter a score: ”; cin >> score; while (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)) { cout << “Invalid score: ” << score; cout << “Enter a score: ”; cin >> score; } }

  18. Data Type bool bool badScore; // Boolean variable with two possible values: true or false badScore = false; // lower case cin >> score; badScore = (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)); while (badScore == true) { cout << “Invalid score: “ << score; cin >> score; badScore = (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)); }

  19. Data Type bool (II) bool badScore; badScore = false; cin >> score; badScore = (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)) // Change (badScore == true) to badScore while (badScore) { cout << “Invalid score: “ << score; cin >> score; badScore = (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)) }

  20. Event-Controlled Loops Sentinel-Controlled Loops Boolean-Variable-Controlled Loops Others

  21. Schedule Quiz3-1 5 PM Today Lab1 Thursday

  22. Program 1 Due February 16 Do it yourself! NONE Differences Style You could lose up to 5 points on style! Programming Rules Checking Style in Lab Thursday.

  23. Where to define constants? Before main() #include <iostream> using namespace std; const float MAX_HOURS = 40.0; const float OVERTIME = 1.5; // Cannot compile! const string prompt = “Enter the hours: ”; int main() { float payRate, hours, wages; string prompt = “Enter the hours: ”; // Not Magic number! cout << “Enter the rate: ”; . . . return 0; }

More Related