3/01 ! includes style reminders not given in lecture ! includes some notes (remarks) not given in lecture Announcements + makeup janosi section? watch newsgroup... + graded P3 available by weekend + P4 has only 3 parts: + turn in solution to original P3.1 + turn in solution to P3.4 + get caught up! (nothing to turn in) P3, logical arrays, vectorization, invariants, ... + E6 scores (CB) re-sent wed night + won't show up online until prelim T2 + recall: core exercise scores are usually out of 1 point + contact me for questions about exercise grades + animated E7 solution by end of weekend + E8 available by weekend Reminders ! style + some student rewrites have been posted to newsgroup + course and t1 eval: save anonymous 7-digit ID and use same one on all 5 parts + lecture feedback + turn in lecture Qs + p4; online submission only, no hardcopy or e-mail Topics + techniques for processing sequences: + invariant (picture), answer so far, previous value + input with stopping value, vector + accumulation examples: sum, max + example: leftmost mode of pre-sorted sequence _______________________________________________________________________________ ! indentation: indent bodies of loops and conditionals ! ! commenting style ! + each major variable should have a definition: ! + comment off to the side, or ! + comment above, initialization indented underneath ! + each loop should have a loop invariant: ! + comment above, at same indentation level ! + a non-trivial sequence of code should have a statement comment: ! + comment above, code indented underneath ! + except for variable definitions, ! you generally should not have comments off to the side ! + comments should be helpful, informative, ! e.g. "initialize variables" is usually neither helpful nor informative _______________________________________________________________________________ processing a sequence (non-vectorized code) + typically + sequence is split into two segments: processed and unprocessed + have "answer so far", based on processed segment + initially, nothing processed, everything unprocessed answer so far | ^ | / \ |----------------------------+ | unprocessed | |----------------------------+ | | + in the middle, some processed, some unprocessed answer so far ___________ | / \| +-------------|--------------+ | processed | unprocessed | +-------------|--------------+ | | + finally, everything processed, nothing unprocessed answer so far = final answer __________________________ | / \| +----------------------------| | processed | +----------------------------| | | + goal: increase processed segment to encompass entire sequence while maintaining "answer so far": at the end, the answer so far will be the final answer _______________________________________________________________________________ example: sum of vector i = 1; x = 0; % x = sum(y(1:i-1)) % inv: picture: % % x = sum so far | i % +---------------|---+-------+ % | | | | % +---------------|---+-------+ % | while i ~= length(y)+1 x = x + y(i); i = i+1; end note: could put position $i$ either to the left or the right of the boundary; here it is to the right ! note: loop body could also be ! ! i = i+1; ! x = x + y(i-1); _______________________________________________________________________________ example: sum of 0-terminated input sequence n = input; % current input value x = 0; % sum so far of input values before $n$ (excluding $n$) % inv: picture: % % x = sum so far | % +---------------|---+-------+ % | | n | | 0 % +---------------|---+-------+ % | while n ~= 0 x = x + n; n = input; end _______________________________________________________________________________ example: max of vector x = -inf; i = 0; % x = max(y(1:i)) % inv: picture: % % x = max so far i | % +---------------+---+-------+ % | | | | % +---------------+---+-------+ % | while i ~= length(y) % [3/12] fixed typo: was $i ~= length(y)+1$ i = i+1; x = x + y(i); end note: could put position $i$ either to the left or the right of the boundary; here it is to the left note: loop body could also be x = x + y(i+1); i = i+1; _______________________________________________________________________________ additional technique: maintain "previous value" example: P3.1, leftmost mode of presorted, (-1)-terminated input sequence + need mode so far *and* its frequency + still not enough: also need previous value and its frequency n = input; % current input value mode.freq = 0; % leftmost mode so far is mode.val with frequency mode.freq prev.val = nan; % previous input value prev.freq = 0; % frequency of prev.val % inv: picture: % % mode.val \ so | % mode.freq / far | % _______________ | % / \| % +-----------+-+-+-|---+--------------+ % | processed | | | | n | unprocessed | % +-----------+-+-+-|---+--------------+ % \____/| % prev.val | % prev.freq | while n ~= -1 % update $prev$ if n == prev.val prev.freq = 1 + prev.freq; else prev.freq = 1; end prev.val = n; % update $mode$ if prev.freq > mode.freq mode = prev; end % read next input value n = input; end note: + observe reuse of ideas from max: $if prev.freq > mode.freq, mode.freq = prev.freq;$ + need ideas of "answer so far" and "(info about) previous value" for P4.1. ! + how does this get the *leftmost* mode instead of some other mode? ! + how would we get the *rightmost* mode instead of the leftmost? ! ! answer: to get rightmost, use $>=$ instead of $>$ to update $mode$: ! ! % update $mode$ ! if prev.freq >= mode.freq ! mode = prev; ! end _______________________________________________________________________________ Q: can you use a character as a stopping value? A: yes, but you need to put it in quotes, and its numerical representation A: --like other stopping values-- must not be a potentially valid input value. Q: what does $nan$ represent? can you just type it in? A: *N*ot *A* *N*umber -- it represents a nonsense/impossible value. yes. Q: why use $i-1$? why not just $i$? A: $i$ can be on either side of the boundary between processed and unprocessed. A: in the first example, it is to the right of the boundary, so the positions A: of elements to the left of the boundary are $1:(i-1)$.