1/25 NOTE: 2/01 -- fixed the rules for doing an assignment. steps 2 and 3 were in the wrong order. Outline + reminder: registration, waiver, demo, P1 + 1:25 sections way too full -- we will do something... + feedback mechanisms + building programs review NEW + variables and assignment tracing: memory model, tabular + sequencing, semicolon to supress output comma + Quiz + (for-)loops loop template _______________________________________________________________________________ please ask questions in lecture! i will give bonus points for asking good questions in lecture -- at the end of lecture, please hand in a piece of paper with your name and short paraphrases of your questions. note: if you have an advanced question, then i am interested in answering it, but please post it to the newsgroup instead of asking in lecture. lecture evaluations i plan to start having online evaluation form for each lecture. you can be anonymous or give your netid, in which case i will give you a bonus point. ------------------------------------------------------------------------------- principle 1: learn and use strategies to avoid pitfalls + ex: start early + ex: read all instructions + ex: pronounce assignment "=" as "gets", "becomes", or "is assigned", NEVER "equals" principle 2: repetitious code is an opportunity for improvement + ex: do not overparenthesize + ex: variables and abstraction help reduce redundancy + ex: loops help reduce redundancy principle 3: indent substructures + ex: indent loop bodies + ex: indent conditionals _______________________________________________________________________________ review: ways to build bigger programs out of smaller ones + building blocks: assignment, ... <-- seen already + sequential execution <-- seen already + iterative (repeated) execution <-- seen already (for-loop) + conditional execution <-- new concept, to be done tue 1/30 _______________________________________________________________________________ review: variables and assignment, semicolon to suppress printing new: tracing, comma + comma, semicolon: >> x = 5, y = x + 1, x = 2 * y, z = y, z = 6 >> x = 5; y = x + 1; x = 2 * y; z = y; z = 6; disp(z) memory model: trace execution + first assignment, declare and initialize variable + reference to non-existent variable: ERROR! + evaluate lhs <-- rhs + evaluate lhs to get location + evaluate rhs to get value <-- 2/01: swapped these + declare variable/location if necessary <-- two lines into right order + put value into location + cross out old value, even if the same + write new value inside box or nearby note: for now, easy to get location -- later, will require computation peek/analogy: + the room Olin 155 versus the address "Olin 155" + the room is huge, cannot fit on a post-it + the address can easily fit on a post-it + finding location of "Olin 155" (versus Olin 255) takes computation -- need to search for correct room tabular format: trace execution again + mostly ignore declaration + one row for each variable + one column for each assignment + even if value is not changed + even if multiple assignments are on the same line + only write in value assigned: leave everything else blank (easier for you to write and for all of us to read) + in a column, to find value for a blank entry, start in that column and scan right-to-left until hit a value + java note: java uses commas and semicolons differently + statements do not produce output by default + there are separate printing commands + each statement must end with a semicolon (pascal note: semicolon in java is statement terminator, not separator) _______________________________________________________________________________ Quiz E1 write your name, cuid number, "Quiz E1" at the top. give pseudocode to perform the following: simultaneously x <-- y, y <-- z, z <-- x ex: if initially x is 3, y is 5, z is 7, then the result has z is 3, x is 5, y is 7 (but your code should work no matter what values x,y,z initially have) hint: suppose you had 3 cups + x holds juice + y holds milk + z holds coffee how to do you rearrange the contents of the cups using another cup tmp? hint: what if you swap 2 of them? bonus let f(x) stand for x + 1/x. use a loop in Matlab code to + silently compute y = f(3) + f(-7) + f(2) + f(8) + f(2.9) + f(1000000) + print the answer y silently means don't display any computated values except the answer y. _______________________________________________________________________________ solution 1: recall to swap a, b: tmp <-- a; a <-- b; b <-- tmp; a) swap x, y; swap z, y b) swap x, z; swap y, x c) swap y, z; swap x, z [2/07] remarks: a *temporary* or *auxiliary* or *helper* variable ($tmp$ in the code above) is necessary. in pseudocode, you should use $<--$ instead of $=$ to differentiate the difference from equality, which is symmetric. solution 2: tmp <-- x; x <-- y; y <-- z; z <-- tmp; general loop template: solution to bonus: start >> y = 0; for each value >> for x = [3 -7 2 8 2.9 1e6] process it >> y = y + x + 1/x; >> end finish >> y [2/07] remarks: the $for$-loop and accumulator $y$ were required for credit. trace question: what if wanted f(3) * f(-7) * ...? answer: start off y = 1;