reminders + newsgroups, webpage, registration form, waiver form + bonus for asking questions + online evaluations for lecture, projects; results posted announcements + turn in p1 into accordion + quiz E1 returned later... + tracing exercise E2 posted later today, due in lecture thursday + p2 posted later today -- start early! + 1:25 sections too full... topics + 3 more principles + 2 corollaries + structs: creation, access (dot notation) <-- disconnected topic for now + (nested loops) + loop template for accumulation + conditionals + vectors: creation, access + random numbers _______________________________________________________________________________ principle: group related things principle: decompose a large problem into smaller problems principle (separation of concerns): work on separate things separately corollary: decompose into small, (as) independent (as possible) + independent: can change one without changing the other, e.g. stereo equipment + small: easier to keep track of principle: make sure everyone agrees/understands on specifications + IF start with *preconditions* (assumptions) true + THEN guaranteed to end with *postconditions* true corollary: don't forget boundary conditions, aka degenerate cases ------------------------------------------------------------------------------- group related things: structs, vectors "new" datatype: struct = collection of named data suppose want to group my age, height, weight: + vector (see later) bad: [17 68 170]? [17 170 68]? [68 17 170]? etc. + want *named* *fields*: PICTURE >> tky.age = 17 >> tky.height = 68 >> tky.weight = 170 or >> tky = struct('age',17, 'height',68, 'weight',170); >> x = tky.weight / tky.height notes: + order of fields does not matter + fields could also be structs! (or vectors) + vector of structs with exactly same field names principles: + reduce redundancy -- namespace: simpler, fewer names to remember + group related things -- conceptual: treat group as a single unit + reduce redundancy -- convenience: 1 assignment/occurrence in place of many + avoid errors -- safety: fewer details = fewer chances for mistakes _______________________________________________________________________________ problem: find max frequency of shared birthdays preconditions: people are trustworthy postcondition: find max freq and *a* corresponding birthday (*a* versus *earliest* or *latest*) people algorithm: group people by the the month of their birthday for each group get a volunteer to stand in front for each day 1..31 announce day find frequency for each month find max of each group now, let us elaborate on the two "find"s. find max of each group: ask jan volunteer for max freq and date and record them for each month feb..dec ask volunteer for max if new max, then record new max and date observations: + when put back into context, have *nested* loops + need *conditional* (given day) find frequency (of month/day) for each month: for each group jan+may+sep, feb+jun+oct, mar+jul+nov apr+aug+dec announce months ask people to raise their hands if their birthday is on day have volunteer count people with raised hands if have new max, then record date and frequency principle (separation of concerns): parallelism can increase speed _______________________________________________________________________________ conditionals + general loop template suppose wish to accumulate a value (sum, max, prod): accumulator = identity; % principle: boundary conditions for value = range accumulator = update(accumulator, value); end sum example (bonus from quiz e1): y = 0; for x = [3 -7 2 8 2.9 1e6] y = y + (x + 1/x); end prod example: y = 1, y = y * (x + 1/x) max example: y = -inf, y = max(y, x+1/x) suppose had no max: principle (reduce redundancy): % y = max(y, x+1/x) % y = max(y, x+1/x) z = x + 1/x; if x + 1/x > y if z > y y = x + 1/x; y = z; end end principal: indent conditionals syntax: if end, if else end, if (elseif)* (else)? end <-- section _______________________________________________________________________________ vectors (ordered lists, numbered variables) motivation: computer simulation/experiment to answer birthday problem algorithm: + randomly throw N darts into 365 buckets + fix max # darts in any bucket questions: + 365 buckets? + random? <-- later vector creation + [] is empty + scalar is 1-by-1 + (horizontal) concatenation by juxtaposition within [ ], e.g. [3 -7 2 8 2.9 1e6] + lo:hi, lo:step:hi, e.g. 1:31, 0:10:100 + zeros(1,n), ones(1,n) vector access + as loop range + x(i), i in range 1..length(x) examples: to display contents of x >> x or >> for i = x >> i >> end or >> for i = 1:length(x) >> x(i) >> end _______________________________________________________________________________ Q: to compute the sum of a list, why do you use $y=0; for j=x, y=y+j; end$ instead of just $y=0+x$? A: as seen in the 2/01 lecture, $0+x$ means "add 0 to each element of x", i.e. gives you back $x$ instead of $sum(x)$. Q: after $z = [x y];$, if you change $x$ or $y$, does $z$ also change? A: no. the fixed rules i gave for executing an assignment statement are pretty precise: observe that an assignment statement does not modify all the places a variable is used. Q: when using $if$-$elseif$, if the initial $if$ condition is true, are the other conditions checked? A: no. the conditions are evaluated sequentially until one is true, at which point the corresponding body of code is executed and the rest of the $if$-$elseif$ is skipped. Q: how do you have 3 $if$ statements in a row, each testing its own condition and executing own body if true? A: just do them in sequence, e.g suppose for each number $n$ in a list $x$ we want to print whether it is divisible by 2, 3, and/or 5, e.g. for $x$ = [4 6 7 30 10] we should get something like: 4 2, yes 6 2, yes 3, yes 7 30 2, yes 3, yes 5, yes 10 2, yes 5, yes here is the code: x = [4 6 7 30 10]; for n = x disp(n) if rem(n,2) == 0 disp('2, yes') end if rem(n,3) == 0 disp('3, yes') end if rem(n,5) == 0 disp('5, yes') end end observe the redundancy. can you reduce it? (answer below). (you may alter the output slightly to make your life simpler.) Q: can you have more than 1 $if$ statement? A: yes. see above. also, you can nest $if$ statements, i.e. put an $if$ inside another $if$ -- depending on how you do exercise E3, you might want to nest conditionals. Q: does $struct$ both initialize and create the structure? A: yes. Q: with $x=[1 2]; y=[3 4];$, can i store $[x y]$ back into $x$? A1: try it! A2: yes. _______________________________________________________________________________ less redundant version of loop with 3 conditionals above, with slightly more verbose output: x = [4 6 7 30 10]; for n = x disp(n) for divisor = [2 3 5] if rem(n,divisor) == 0 disp('yes:'); disp(divisor); end end end observe that we can now easily alter the list of divisors. _______________________________________________________________________________ bonus: formatted output. we'll get to formatted output soon, but if you want to go ahead, here is how to get output like the original version: x = [4 6 7 30 10]; for n = x disp(n) for divisor = [2 3 5] if rem(n,divisor) == 0 fprintf('%d, yes\n', divisor); end end end