CS 1430: Programming in C++ No time to cover HiC
Tracing 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: 48 54 66 53 59 -1 score max ? 0 48 54 66 53 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!
Quiz3-2 10 Minutes
Write C++ Statements // Decrease theCount by 2 until theCount is negative. while ( theCount >= 0 ) { theCount -= 2; } // Another way while ( !(theCount < 0) ) // Incorrect! while ( theCount !< 0 ) while ( theCount > 0 ) Slides 21-23 could be moved to an earlier note
Write C++ Statements // While theValue is not positive, increase its value by 5. while ( theValue <= 0 ) { theValue += 5; } // Another way while ( !(theValue > 0) ) // Incorrect! while ( theValue < 0 )
Write C++ Statements DeMorgan’s Law // While theValue is not positive and theCount is // less than 10, increase theValue by 5 and theCount by 1. while ( theValue <= 0 && theCount < 10 ) { theValue += 5; theCount ++; } // increase theValue by 5 and theCount by 1 // until theValue is positive or theCount is 10 or higher. while ( !(theValue > 0 || theCount >= 10) ) // The same? DeMorgan’s Law
Example int someValue = 0; int i = 0; while (i < 10) { int j = 0; while (j < 5) someValue += i * j; // A j ++; } i ++; How many times is statement A executed? 10 5 50 15 None of above 50
Example int count = 0; int i = 0; while (i < 10) { int j = 5; while (j > 0) count ++; j --; } i ++; What is the final value of count? 10 20 50 100 None of above 50
Example int total = 0; int i = 5; while (i > 0) { int j = i; while (j > 0) total += j; j --; } i --; What is the final value of total? 15 25 35 45 None of above 35
Tracing Nested Loops int xValue, yValue, zValue; xValue = 3; while (xValue > 1) { zValue = 1; yValue = xValue; while (yValue > 0) zValue *= yValue; yValue --; } cout << "What is this: " << zValue; xValue --; xValue yValue zValue ? ? ? 3 1 2 6
Schedule Quiz3-3 Lab3 Prog1 Due 10 pm today Due 5 pm Thursday Past Due already Grace Time: 10 pm Friday
Schedule Prog2 Test 1 Nested loops Started Tuesday, 9-22 Friday, October 2 60 points If, While, and Others
Getting Help Pals 9 – 3 pm, Thursdays Lab Assistants 6 – 9 pm, Mon - Thur Come to me for help Name your file: UserName_Lab/ProgX Drop your program in K:\Courses\CSSE\yangq\Dropbox
You can still receive 15 points on Prog1!