% CS99 Fall 2002 % Prelim 1 % Problem 1 %------------------------------------------------------------------------------ % 1a [1 point] Your lab instructor's name is GUN SRIJUNTONGSIRI. % 1b [1 point] Your lecture instructor's name is DAVID I. SCHWARTZ. % 1c [1 point] Programming is AUTOMATING PROBLEM SOLVING. % 1d [2 points] MATLAB stands for MATRIX LABORATORY. % 1e [1 point] True or False: Everything in MATLAB is an array: TRUE. % 1f [1 point] True or False: % The assignment statements a=10 and A=10 are identical: TRUE. % 1g [2 points] Demonstrate how to remove an assigned value from % the variable x from the command window. clear x % or just CLEAR % 1h [3 points] Show one expression statement that will evaluate: 17 * sqrt( 2 + 3 / ( 4 - 1 ) ) % 1i [2 points] What is the output from the following statements? x = 1; y = x + 1, z = x % output: y = 2 % z = 1 % 1j [2 points] What is the output from the following statement? ~(1 ~= ~1) | 1 % output: 1 % 1h [4 points] Write a MATLAB expression that generates a random % integer between -2 and 1, inclusive. % formula: floor(rand*(max-min+1)) + floor(min) % do some arithmetic: floor(rand*(1-(-2)+1)) + floor(-2) floor(rand*4) - 2 % CS99 Fall 2002 % Prelim 1 % Problem 2 %------------------------------------------------------------------------------ % 2a Algorithm: % Set number of rolls so far to zero. % Set number of snake-eyes so far to zero. % If count of rolls is not exceeded (100), roll both dice. % + If dice1 and dice2 rolled 1, increment count of snake-eyes. % + Increment count of rolls. % + Repeat. % Report the number of snake-eyes out of 100 rolls. % % 2b Solution: % Initialize variables: maxrolls = 100; % max number of allowed rolls count = 0; % number of rolls so far snakeeyes = 0; % number of snake-eyes so far % Roll both dice and count snakeeyes for maxrolls: for count=1:maxrolls count=count+1; roll1=floor(rand*6)+1; % roll 1st die roll2=floor(rand*6)+1; % roll 2nd die % Count snake-eyes: if roll1==1 & roll2==1 snakeeyes=snakeeyes+1; end end % Report chance of rolling snake-eyes disp(['Chance of rolling snake-eyes: ',num2str(100*snakeeyes/maxrolls),'%']); % CS99 Fall 2002 % Prelim 1 % Problem 3 %------------------------------------------------------------------------------ % Initialize variables: value1 = input('Enter a number: '); value2 = input('Enter another number: '); % Pick max and min: if value1>value2 max=value1; min=value2; else max=value2; min=value1; end % Report even values between min and max: for ii=min:max % Find even number: if mod(ii,2)==0 disp(num2str(ii)); end end