2/13 lecture announcements + prelim rooms: Olin Hall 155: A - L Olin Hall 255: M - Z + come early: outer doors will be locked (we'll try to periodically check) + online submission system is up: + e-mail tyan@cs.cornell.edu if you did not receive e-mail with password + try out with E4 (don't hand anything in) + don't try to submit P3 until next week + prelim T1 pre-evaluation form: tell me what questions i should ask! + P3 readiness: (P3.1 will be revised -- too hard) P3.1, P3.2, P3.4 -- you are not fully ready P3.3 -- you should be ready P3.5 -- you should be ready to redo P2.1, possibly to redo P2.3 note: even if it is too early for you to program, it is *not* too early to read the problem and start thinking about it! topics (yes, logic is important!) + $input$ + $while$ loops + processing input with a stopping value + review: logical values and operators + review: logical quantifiers + review: demorgan's law future: + example: sieve of eratosthenes + functions, matlab path + formatted output _______________________________________________________________________________ $while$ loops + matlab syntax: while $condition$ <-- loop *guard* or loop *test* or loop *condition* $statements$ <-- loop *body* end + execution: 1. evaluate loop guard 2. if true, then execute loop body and then go back to step 1. 3. otherwise, continue execution after $end$ + observe: loop body is executed 0 or more times + general template (pseudocode): set things up while not done advance finish things up + observe: want opposite condition of $done$ _______________________________________________________________________________ example, with picture (loop storyline): % print elements of x one at a time n = length(x); % total #elements to print i = 0; % #elements already printed % done = printed all elements = i == n while i ~= n % note: inefficient to use $length(x)$ in place of n! i = i+1; x(i) end _______________________________________________________________________________ example, with picture (loop storyline): % print elements of x in reverse order i = length(x); % remaining #elements to print % done = printed all = none remaining = i == 0 while i ~= 0 x(i) i = i-1; end _______________________________________________________________________________ processing input with a stopping value general template (pseudocode): set things up read first value while value ~= stoppingValue process value read next value finish up example: read sequence, terminated by -1, report sum stoppingValue = -1; prompt = 'value (-1 to stop)? '; total = 0; % sum so far, not including num num = input(prompt); while num ~= stoppingValue total = total + num; num = input(prompt); end total observe: no need to store all the numbers! _______________________________________________________________________________ logical values + canonical true = 1: $2 < 3$ + canonical false = 0: $2 == 3$ + $&$ and $|$ are like $*$ and $+$: evaluate operands, then evaluate operation $2<3 | 2==3$ --> $1 | 0$ --> $1$ $2<3 & 2==3$ --> $1 & 0$ --> $0$ + truth tables or false true and false true xor false true +------------ +------------ +------------ false | false true false | false false false | false true true | true true true | false true true | true false plus 0 1 times 0 1 plusMod2 0 1 +----- +------ +----- 0 | 0 1 0 | 0 0 0 | 0 1 1 | 1 2 1 | 0 1 1 | 1 0 0+x = x+0 = x false or C = C or false = C 1*x = x*1 = x true and C = C and true = C 0*x = x*0 = 0 false and C = C and false = false true or C = C or true = C let C be any condition, e.g. "tky hates shrimp" or "tky likes BTVS" it is 1999 or C = C feb 13, 2001 is a tuesday and C = C _______________________________________________________________________________ quantifiers: sometimes need to accumulate truth values + $any(cc)$ = "big or" = "there exists" = "some": true if there is a true in $cc$, false otherwise + $all(cc)$ = "big and" = "for all": false if there is a false in $cc$, true otherwise + example: vector equality: $all(x == y)$ + example: vector inequality: $any(x ~= y)$ _______________________________________________________________________________ demorgan's laws: sometimes need opposite of condition (e.g. for loop guard) + can use $~$ + sometimes clearer to eliminate/avoid $~$ + not(spoon and fork) = not spoon or not fork + not(spoon or fork) = not spoon and not fork + ~any(cc) = all(~cc): e.g. not(some people happy) = all people not happy + ~all(cc) = any(~cc): e.g. not(all people happy) = some people not happy _______________________________________________________________________________ preview of next lecture ! sieve of eratosthenes (for listing primes up to N) ! + note: 1 is a *unit*, a number with a multiplicative inverse (reciprocal) ! + units are not considered to be primes ! ! algorithm: ! ! list integers from 1 to N ! cross off 1 ! for each integer i from 1 to N ! if not crossed off then ! cross off multiples of i in list (excluding i) ! list non-crossed-off numbers ! ! note: can use $input$ to initialize $N$. ! ! recall Question: what should we do so that so remember what arrays are logical? ! A: 1. remember that logical operations and functions return logical values ! A: 2. maybe name variables with "is", e.g. "ishappy" ! A: 3. when you initialize a variable, include a comment ! ! >> N = input('please enter an integer N>=1: '); ! >> list = logical(ones(1,N)); % list(i) == "i is *not* crossed off?" ! >> % note: $logical$ is optional above ! >> list(1) = 0; % cross 1 off ! >> for i = 1:N ! >> if list(i) ! >> list(2*i:i:N) = 0; ! >> end ! >> end ! >> primesVersion1 = find(list) ! ! revised algorithm: ! ! list integers from 2 to N ! create empty list $primes$ of primes found so far ! while list is non-empty ! i <-- first element of list ! put i on list $primes$ ! delete *all* multiples of i from list (including i) ! display list $primes$ ! ! matlab code: ! ! >> list = 2:N; ! >> primes = []; ! >> while ~isempty(list) ! >> i = list(1); ! >> primes = [primes i]; ! >> list(rem(list,i) == 0) = []; ! >> end ! >> primesVersion2 ! ! remark: if run both versions, can test to see if they agree: ! ! >> if any(primesVersion1 ~= primesVersion2) ! >> error('versions 1 and 2 of sieve disagree!') ! >> end _______________________________________________________________________________ questions asked in lecture: Q1: having two $input$s is redundant -- is there any way to have only one? Q2: can you evaluate the loop body before the loop guard? A1: no. A2: yes, but it is a bit clumsy. we'll see it later. A3: other languages, e.g. java, have a $do$-$while$ or $repeat$-$until$ loop. A4: most languages are missing a nice loop construct for "loops-and-a-half", A4: which execute code, test the condition, execute more code, then repeat: A4: A4: wish> loop A4: wish> $statements$ A4: wish> continueIf $condition$ A4: wish> $statements$ A4: wish> end Q: in the 2nd while loop to print x in reverse order, Q: could you instead use fliplr and the original while loop Q: (print x in left-to-right order)? A: yes Q: why should programmers care about logical identities, demorgan's laws, etc.? A1: you need to know how logical operations work in order A1: to read and understand other people's code. A2: when writing programs, you often have to deal with compound conditions A2: and opposites of conditions. to obtain clear and concise code, you need A2: to use logical identies, etc., to simplify logical expressions.