CS99, Fall 2002 Mon 10/28 Lecture 9 ------------------------------------------------------------------------------- (0) Announcements: + reading: posted in Lecture 8 + HW6 due Nov 4 + HW6's problem 1 changed! Topics: + designing loops continued + nested control structures ------------------------------------------------------------------------------- (1) Designing a loop + General programming: - Brainstorm and research phase - Write algorithm - Determine classification of loop + Pattern to follow: - initialize variables (from research/brainstorm phases) + boundary conditions? + what are LIMITS to the data you need to work with? - get values to test - if test conditions are true + account for the values you obtained + update new values + get next value(s) to test - repeat - aftermath: + account for never entering the loop or other "boundary conditions" + account for data that did get processed in loop ------------------------------------------------------------------------------- (2) Examples of common patterns for indefinite loops % ECHO: v = input('enter value: ') while v ~= stop disp(v); g = input('enter value: '); end % SUMMATION: stop = -1; sum = 0; v = input('enter value: '); while v ~= stop sum = sum+v; v = input('enter value: '); end if sum==0 disp('no values'); else sum end % CONDITIONAL UPDATE: max=-1; v = input('enter value: '); while v ~= stop if v > max max=v; end v = input('enter value: '); end if max == -1 disp('no values'), else max end ------------------------------------------------------------------------------- (3) Nested control structures + selection and repetition statements ARE indeed statements + so, they can be statements inside the block of another control statement + You've seen selection inside repetition + Now we cover repetition inside repetition ------------------------------------------------------------------------------- (4) Nested loops + a while/for statement "within" another + used for multidimensional arrays, reprompting input, character graphics, tests of *other* repetitive tasks (simulations) + designing: - think "inside out" - design the interior loop(s) and then the outer loops ------------------------------------------------------------------------------- % array example a=[ 1 2 3; 4 5 6]; % create 2D array [rows cols] = size(a); % get row and column sizes of array % print array: for ____ = ______ : _______ for ____ = ______ : _______ fprintf(' %2.0f',________________ ); % print a row one elem at a time end fprintf(________) % skip a line end fprintf('\n') % print columns of arrays as rows (called a transpose): for ____ = ______ : _______ for ____ = ______ : _______ fprintf(' %2.0f',a(_____,_____)) end fprintf('\n') end ------------------------------------------------------------------------------- % character graphics example: rows = 4; cols = rows; for ____ = ______ : _______ % Print one row: for ____ = ______ : _______ % Choose which row to print: if ___________________ fprintf('/\\'); else fprintf('\\/'); end end % Skip row fprintf('\n'); end -------------------------------------------------------------------------------