Download presentation
Presentation is loading. Please wait.
Published byΌσιρις Αξιώτης Modified over 6 years ago
1
CS 1430: Programming in C++ No time to cover HiC
2
Quiz2-2 1. For each statement in the following, determine if it’s valid or invalid. (Circle your answers.) float rate, hours, wage; cin >> rate >> hours; rate * hours = wage; wage = rate * hours; cout >> “The wage is ” >> wage; cout << “The wage is ” << wage; // Valid Invalid
3
Quiz2-2 2. What integer value does the following byte represent? 102 Assume the above byte represents ASCII character ‘f’. Gave the byte representing ASCII character ‘c’. 99 1 1
4
Quiz2-2 3. Evaluate the following mathematical expressions in C++:
15 / 8 19 % 33 / 3 * 2 6 + 4 * 2 6 + 8 Result 1 19 14
5
Quiz2-2 4. Write C++ statements according to the comments.
// 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;
6
Quiz2-2 4. Write C++ statements according to the comments.
// 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!”;
7
Quiz2-2 4. Write C++ statements according to the comments.
// Display cost at the beginning of next line with // message “The cost is ” cout << endl << “The cost is ” << cost; cout << ‘\n’ << “The cost is ” << cost; cout << “\n” << “The cost is ” << cost; cout << “\nThe cost is ” << cost;
8
Sentinel-Controlled Loop
const int END_VALUE = -1; // not required const int LOW_LIMIT = 0; // not required const int HIGH_LIMIT = 60; int main() { 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; cout << “The max score is ” << max << ‘.’; return 0;
9
Sentinel-Controlled Loop
const int END_VALUE = -1; // not required const int LOW_LIMIT = 0; // not required const int HIGH_LIMIT = 60; int main() { int score, max; max = 0; // Range is known cin >> score; // Prime read while (score != -1) if (score >= 0 && score <= 60) // 0.5 points off on Prog1! if (score > max) max = score; } else cout << “Invalid score!”; cin >> score; cout << “The max score is ” << max << ‘.’; return 0;
10
Loop Control Variable (LCV)
while (score != -1) { … } LCV: score When to exit a loop Avoid infinite loops
11
Four Parts of Loops Loop Initialization (LCV and others) Testing LCV
Loop Body Updating LCV
12
While Loop Initialization (LCV and others) while (LogicalExpression)
(Testing LCV) false true Do Work Update LCV Statement after loop
13
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; cout << “The max score is ” << max << “.”;
14
Computing Count Range is not Known int score, max, count;
count = 0; // initialization // cannot initialize max, since range unknown cin >> score; // Prime Read while (score != -1) { count ++; // count = count + 1; if (count == 1) // first score max = score; else if (score > max) } cin >> score; if (count == 0) cout << “No scores!”; cout << “The maximal score is ” << max;
15
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) } cin >> score; if (count == 0) cout << “No scores!”; cout << “The maximal score is ” << max; // Does it run? // Run time error! // Uninitialized value: count!
16
Computing Count Range is not Known int score, max, count;
count = 0; // Initialization! cin >> score; while (score != -1) { count ++; if (count == 1) max = score; else if (score > max) } if (count == 0) cout << “No scores!”; cout << “The maximal score is ” << max; // Works even there is no score
17
Trace in Visual Studio Project CS1430Trace Trace-RangeUnkown.cpp
Trace-RangeKnown.cpp Line/Column number Break Point Step Over Reset Auto/Locals AutoHide
18
Trace Execution on Paper
Range unknown int score, max, count; count = 0; cin >> score; while (score != -1) { count ++; if (count == 1) max = score; else if (score > max) } if (count == 0) cout << “No scores!”; cout << “The maximal score is ” << max; Input Scores: score max count ? ? ?
19
Tracing on Paper Range Known Tracing Input: 48 54 66 53 59 -1
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; } Tracing Input: score max ? 48 54 66 53 59 -1 How to do it? New values on next line Do not cross old values Blank when no new value WILL BE ON QUIZ And Test 1 And Final!
20
Checking Input Range max = 0; cin >> score; while (score != -1) { if (score >= 0 && score <= 60) if (score > max) max = score; else cout << “Invalid score!”; } // Is it correct? //(ignoring magic nubers) // Which if is else paired with? max = 0; cin >> score; while (score != -1) { if (score >= 0 && score <= 60) if (score > max) max = score; else cout << “Invalid score!”; } //else is paired with the last if!
21
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;
22
Getting a Valid Value 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!”; Correct?
23
Getting a Valid Value Pseudo Code Input a value
While the input value is out of range Display a message cin >> score; while (score < 0 || score > 60) { cout << “Invalid score!”; } How about the end value -1?
24
Getting a Valid Value Pseudo Code Input a value
While the input value is out of range and not -1 Display a message cin >> score; while ((score < 0 || score > 60) && score != -1) { cout << “Invalid score!”; } // Is the following correct? while (score < 0 || score > 60 && score != -1) // NO! It’s the same as the following: while (score < 0 || (score > 60 && score != -1))
25
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; } while (score != END_VALUE) if (score > max) max = score;
26
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; }
27
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; }
28
Event-Controlled Loops
Sentinel-Controlled Loops Boolean-Variable-Controlled Loops Others
29
Quiz3-1 10 PM Today Lab2 Yesterday (5) Next Tuesday (3) Prog1
Schedule Quiz PM Today Lab2 Yesterday (5) Next Tuesday (3) Prog1
30
Program 1 Due Tuesday September 22 by 10 pm: 15 points
(two bonus points towards style) Grace Friday September 25 by 10 pm: 15 points After Grace Time: 0 points and you still have to do it! NONE Differences: at least on test 1 (otherwise: F for CS1430) Do it yourself!
31
Programming Grand Rules
You could lose up to 7 points on style! I can check your Style if you print it out or put it into the DropBox.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.