CS100 Midterm 2009

# 6points

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3 times 2 = 6 3 times 3 = 9 3 times 4 = 12 3 times 5 = 15 3 times 6 = 18 3 times 7 = 21 3 times 8 = 24 3 times 9 = 27 3 times 10 = 30 Table of 4 4 times 1 = 4 4 times 2 = 8 4 times 3 = 12 4 times 4 = 16 4 times 5 = 20 4 times 6 = 24 4 times 7 = 28 4 times 8 = 32 4 times 9 = 36 4 times 10 = 40 11. (6 points) The required output on the figure window is shown below. Fill in the two blanks in the code to generate this output. 100 90 80 70 60 50 40 30 20 10 0 0 20 40 60 80 100 figure axis equal axis([0 100 0 100]); for i = _____________ rectangle('position', [0, i, _______], 'facecolor', 'r'); end; Answer box Code in first blank: _____________________________________________ Code in second blank: ___________________________________________ 12. (6 points) Analyze the code given below and draw the output in the given figure window. figure; axis equal axis([0 100 0 100]); curLoc = [50 50]; for count = 1:10 if floor(count/2) == count/2 newLoc = curLoc + [10 0]; else newLoc = curLoc + [0 -10]; end; line('Xdata', [curLoc(1), newLoc(1)], 'YData', [curLoc(2), newLoc(2)]); curLoc = newLoc; end Answer Box 13. (10 points) The code below is written to generate a bouncing ball program similar to the one attempted in the lab. There is only one ball reflecting off the walls. The portion of the code within the box has a logical error which makes the ball bounce off the walls in an undesired way. Identify the error and suggest a correction. close all figure axis equal axis([0 100 0 100]); curLoc = [50, 50]; curVel = 2 * (rand(1,2) - 0.5); radius = 5; ball = rectangle('position', [curLoc, 2*radius, 2*radius], ... 'curvature', [1 1], 'facecolor', 'g'); while 1 set(ball, 'Position', [curLoc, 2*radius, 2*radius]); curLoc = curLoc + curVel; if (curLoc(1) curVel(1) end; if (curLoc(2) curVel(2) end; pause(0.01) > 100) | (curLoc(1) < 0) = -curVel(1); > 100) | (curLoc(2) < 0) = -curVel(2); end; What is the corrected version of the code highlighted within the box. Answer box: 14. (15 points) figure; axis equal; axis([-5 105 -5 105]); p = [50 50]; rectangle('position',[0 0...
View Full Document

## This test prep was uploaded on 03/02/2014 for the course CS 100 taught by Professor Sohaibkhan,arifzaman,murtazataj during the Spring '11 term at Lahore University of Management Sciences.

Ask a homework question - tutors are online