230 likes | 335 Views
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;
E N D
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!”;
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; }
Loop Control Variable (LCV) while (score != -1) { … } • LCV: score • When to exit a loop • Avoid infinite loops
Four Parts of Loops • Loop Initialization (LCV and others) • Testing LCV • Loop Body • Updating LCV
While Loop Initialization (LCV and others) while (LogicalExpression) (Testing LCV) false true Do Work Update LCV Statement after loop
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; }
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!
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
Trace in HiC HiCTrace.cpp HiCTraceII.cpp Window/Console View/Status Window Line/Column number Break Point Step Over Reset
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
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; }
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?
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; }
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?
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?
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))
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; } }
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)); }
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)) }
Event-Controlled Loops Sentinel-Controlled Loops Boolean-Variable-Controlled Loops Others
Schedule Quiz3-1 5 PM Today Lab1 Thursday
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.
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; }