CS99 Fall 2001 Solutions to Prelim2 Sample Problems +-----------------------------------------------------------------------------+ 1a. Algorithm Spew one box of random size(1 to 5) from chute If the amount of carts(8) and amount of boxes(40) is not exhausted: + If the current box is not a #5 -Place the box on the current cart -Increment total number of boxes on carts + Otherwise -Increment the cart count -Check if the carts are exhausted + If the carts are not exhausted, -Place the current box on the new cart -Increment total number of boxes on carts + Repeat If necesary, adjust the cart count and/or box count to account for whether or not they were incremented too many times Report the number of carts used and total boxes taken +-----------------------------------------------------------------------------+ 1b. Program %Initialize data to test: MIN=1; %named constant for minimum box size MAX=5; %named constant for maximum box size maxBoxes=40; %max # of boxes that the company produces totalBoxes=12; %total # of boxes taken so far cartTotal=8; %total amount of carts cartCount=3; %cart count so far currentBox=floor(rand*(MAX-MIN+1))+floor(MIN); % random size %Attempt to place boxes on carts until boxes or carts are used up: while (cartCount<=cartTotal & totalBoxes+1<=maxBoxes) if (currentBox~=MAX) %Stack boxes on old cart totalBoxes=totalBoxes+1; else %#5 box starts new cart cartCount=cartCount+1; if (cartCount<=cartTotal) %can #5 be stacked? totalBoxes=totalBoxes+1; end end currentBox=floor(rand*(MAX-MIN+1))+floor(MIN); end %Report number of carts used and total number of boxes on all carts: if (cartCount>cartTotal) cartCount=cartCount-1; disp(['Final total: ',num2str(totalBoxes)]); disp(['Final carts: ',num2str(cartCount)]); end % there is a way of compressing the solution even further by changing the % while condition a bit.... +-----------------------------------------------------------------------------+ 2. % Script file: cable.m % % Purpose: % To calculate the connection point that results in % minimum tension on a cable. % % Record of revisions: % Date Programmer Description of change % ==== ========== ===================== % 02/18/00 S. J. Chapman Original code % % Define variables: % dist -- Distance to attachment (ft) % lc -- Cable length (ft) % ii -- index variable % lp -- Pole length (ft) % saved_dist -- Save attachment distance % saved_tension -- Saved tension % tension -- Tension on cable % weight -- Weight of object (lbs) % Initial values lc = 8; lp = 8; weight = 200; clear dist tension % Calculate tension at 0.1 ft dist(1) = 1.0; tension(1) = weight * lc * lp ... / ( dist(1) * sqrt(lp^2 - dist(1)^2) ); % Save these initial values for comparision saved_dist = dist; saved_tension = tension; % Calculate for other values between 1 and 7 ft for ii = 2:60 % Calculate tension dist(ii) = ii/10 + 1; tension(ii) = weight * lc * lp ... / ( dist(ii) * sqrt(lp^2 - dist(ii)^2) ); % Find minimum tension point if tension(ii) < saved_tension saved_tension = tension(ii); saved_dist = dist(ii); end end % Display minimum tension point disp(['Minimum tension at ' num2str(saved_dist)]); disp(['Minimum tension = ' num2str(saved_tension)]); % Plot tension vs distance figure(1); plot(dist,tension,'b-','LineWidth',2); grid on; title('\bfPlot of tension vs attachment distance'); xlabel('\bfDistance'); ylabel('\bfTension'); +-----------------------------------------------------------------------------+ 3. % Script file: geometric_mean.m % % Purpose: % To calculate the geometric mean of a set of positive % numbers. % % Record of revisions: % Date Programmer Description of change % ==== ========== ===================== % 02/26/00 S. J. Chapman Original code % % Define variables: % x -- Input values % g -- Geometric mean % nvals -- Number of input values % prod -- Product of input values % Initialize product and nvals prod = 1; nvals = 0; % Read the first number x = input('Enter first number: '); % Read the remaining numbers while x > 0 prod = prod * x; nvals = nvals + 1; x = input('Enter next number: '); end % Calculate geometric mean g = prod ^ (1/nvals); % Tell user fprintf('The geometric mean is %.4f\n',g); +-----------------------------------------------------------------------------+ 4. radius = str2double(input(' Enter a radius: ','s')); while ~isreal(radius) | isinf(radius) | isnan(radius) | ~isnumeric(radius) disp(' Please enter a real, finite number: '); radius = str2double(input(' Enter a radius: ','s')); end num=1; for theta=0:(2*pi)/200:(2*pi) x(num)=radius*cos(theta); y(num)=radius*sin(theta); num=num+1; end plot(x,y,'-'); +-----------------------------------------------------------------------------+ 5. a=input('Enter a 1-D array: '); [rows,cols]=size(a) A=zeros(cols) rows=cols; %for each row for i=1:rowss % for each column for j=1:cols %1D array on the diagonal A(i,i)=a(i); %"reverse diagonal" A(i,cols-i+1)=a(cols-i+1); end end A +-----------------------------------------------------------------------------+