CS100 Midterm 2009

# Thesecondcircleisdrawnneartheupperlowerleftrightcorner

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: cle is drawn near the upper / lower left / right corner. The second circle is drawn near the upper / lower left / right corner. 5. (3 points) What is the largest possible value of x at the end of this program? [May be a bit tricky!] x = 1; for n=1:20 if rand&lt;10 x = x - 1; elseif rand&lt;0.5 x = x + 1; end end Answer box Largest possible value of x is _________________ 6. (3 points) What is the value of “x” at the end of the loop? x = [1]; while sum(x) &lt; 20 x = [x sum(x)] end; Answer box The value of x at the end of the loop is ________________________________ 7. (3 points) x and y are two variables which contain some values. We wish to write code which will swap the values contained in them. That means that after executing the code, the value originally in x should now be in y and the value originally in y should now be in x. There are several lines of code given below. You have to specify three lines out of this list, and their order, such that, when they are executed, the values of x and y will be swapped. Line 1: x = y Line 2: x = z Line 3: y = x y = z Line 4: Line 5: z = x Line 6: z = y Answer Box Line _____ should be executed first, followed by Line _____ and then by Line _____ 8. (5 points) In a game, I want to generate a fair dice, which will generate integers from 1 to 6 with equal probability. Several possible ways are given below, some of which may be incorrect. In the box next to each line, write “Correct” if the line represents a correct way to generate a fair dice. If the method is incorrect, write a short reason, not longer than a few words, of why it is incorrect. Answer Box d = round(6*rand); d = 1 + round(5 *rand); d = ceil(6 * rand); d = 1 + floor(5 * rand); d = 1 + floor(6 * rand); 9. (6 points) The following program displays eight lines. for x = 1:30 for y = 1:30 if x*y==30 disp([num2str(x) ' times ' num2str(y)]); end end end Write the complete output of the program in the box on the right Answer box 10. (6 points) You have to write a ‘times table’ program which will generate the output shown in the column on the right. Two lines of the code are given in the box below. Fill in the rest of the code which will result in the output shown. Answer Box disp(['Table of ' num2str(t)]); disp([num2str(t) ' times ' num2str(i) ' = ' num2str(r)]); Table of 2 2 times 1 = 2 2 times 2 = 4 2 times 3 = 6 2 times 4 = 8 2 times 5 = 10 2 times 6 = 12 2 times 7 = 14 2 times 8 = 16 2 times 9 = 18 2 times 10 = 20 Table of 3 3 times 1 = 3...
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