% CS99 Fall 2001 % Prelim 2 Solutions %-----------------------------------------------------------------------------% % Problem 1 depth = str2double(input('Enter the depth: ','s')); while ~isreal(depth) | isinf(depth) | isnan(depth) | ~isnumeric(depth) | ... floor(depth)~=ceil(depth) | depth < 1 disp(['Please enter a legal integer!']); depth = str2double(input('Enter the depth: ','s')); end % Approach 1: for row=1:depth % print whitespace: for col=1:depth-row fprintf(' '); end % print stars: for col=1:row*2+1 fprintf('*'); end % advance to the next line: fprintf('\n'); end % Approach 2: for row=1:depth for col=1:depth+row+1 if col<=depth-row fprintf(' '); else fprintf('*'); end end % advance to the next line: fprintf('\n'); end %-----------------------------------------------------------------------------% % Problem 2 values = [1 2 3; 4 5 6; 7 8 9]; % approach 1: [rows cols] = size(values); sums = zeros(1,rows); for col=1:cols for row=1:rows sums(col) = sums(col)+values(row,col); end end sums % approach 2: sums = sum(values); %-----------------------------------------------------------------------------% % Problem 3 % %-----------------------------------------------------------------------------% % Algorithm: % ========= % + Start from a non-moving position % - no calories burned so far % - no cycles performed so far % + Determine if people stop prematurely % + If people haven't decided to stop and the cycles have not finished % - contribute calories from each person % - increment the amount of cycles so far % - determine if the people want to stop prematurely % - repeat % + If the people stopped before playing (no cycles), % - report no cycles and calories burned % + Otherwise, % - report cycles and calories burned %-----------------------------------------------------------------------------% % Initialize variables: MAXCYCLES = 25; % max number of cycles to "play" BOREDOM = 10; % percent of time cycles = 0; % cycles "played" so far CALORIE = 10; % amount of calories per person per cycle calories = 0; % amount of calories consumed so far continueFlag = 1; % flag to signal whether or not to continue % Initial check if people stop early: stopValue = floor(rand*10) + 1; % random number between 1 and 10 if stopValue == 1; continueFlag = 0; end % Add calories for each cycle if the people do not stop prematurely % the cycles are not exhausted: while continueFlag & cycles < MAXCYCLES % Contribute calories from each person: calories = calories+2*CALORIE; % Increment cycles: cycles = cycles + 1; % Check for a premature stop: stopValue = floor(rand*10) + 1; % random number between 1 and 10 if stopValue == 1; continueFlag = 0; end % Repeat: end % Report results: if cycles==0 disp(['The people did not play.']); elseif cycles < MAXCYCLES disp(['The people stopped prematurely.']); else disp(['The people played an entire game.']); end disp(['Amount of calories burned: ',num2str(calories)]); %-----------------------------------------------------------------------------%