CS1112 Fall 2008 Prelim 1 Solutions: Mathematics and Programming Exercise Solutions, Exams of Computer Science

The solutions to various mathematical and programming exercises from the cs1112 fall 2008 prelim 1 exam. The exercises cover topics such as variable assignments, loops, conditional statements, and generating random numbers. Students can use this document as a study resource to understand the concepts and solutions to the problems.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-la2
koofers-user-la2 🇺🇸

5

(1)

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Fall 2008 Prelim 1 Solutions
2
Question 1: (10 points)
Part (a): (3 points)
What are the final values of variables x and y?
x= 4;
y= 8;
x= y;
y= x;
Part (b): (3 points)
What are the final values of variables x and y?
x= 4; y= 8;
if x<5
x= 1;
elseif x<2 && y<10
y= 11;
end
Part (c): (4 points)
Assume that variables a, b, and c store real values and a<b. Write an expression that evaluates to true
if c is in the open interval (a,b) or the interval (a,b) is not bigger than 0.1.
a<c && c<b || b-a<=0.1
x
y
8
8
x
y
1
8
pf3
pf4
pf5

Partial preview of the text

Download CS1112 Fall 2008 Prelim 1 Solutions: Mathematics and Programming Exercise Solutions and more Exams Computer Science in PDF only on Docsity!

Question 1: (10 points)

Part (a): (3 points)

What are the final values of variables x and y?

x= 4; y= 8; x= y; y= x;

Part (b): (3 points)

What are the final values of variables x and y?

x= 4; y= 8; if x< x= 1; elseif x<2 && y< y= 11; end

Part (c): (4 points)

Assume that variables a, b, and c store real values and a<b. Write an expression that evaluates to true if c is in the open interval (a,b) or the interval (a,b) is not bigger than 0.1.

a<c && c<b || b-a<=0.

x

y

x

y

Question 2: (20 points)

Part (a): (10 points)

For each fragment below, write a while-loop to produce the same printed output as the given for- loop. Only the output needs to be the same—any intermediate values need not be the same.

for k= 100:-2: disp(k) end

for k= 5:2: disp(k) k= 200; end

Part (b): (10 points)

Let x and n be variables that store positive integer values. Without using the ^ operator or the equivalent built-in function power, write a fragment to print the values x , x^2 , x^3 , …, x n. Any print format is acceptable.

val= 1;

for k= 1:n

val= val*x;

disp(val)

end

Grading notes:

OK to print by leaving out semi-colon

Check that boundary case n=1 is correct

k= 100;

while k>=5 % or k>5, k>=

disp(k)

k= k-2;

end

k= 5;

while k<=100 % or k<100, k<=

disp(k)

k= k+2;

end

Some students literally replaced the DrawDisk function with an equivalent DrawSquare

function. The parameter “radius” is then the “half length” of the square, which is r/2 in

the code. We accepted it.

% Draw the SQUARE in the appropriate color

if abs(x)+r/2<1 && abs(y)+r/2<1 % inside black square

DrawSquare(x,y,r/2,'r')

elseif abs(x)-r/2>1 || abs(y)-r/2>1 % outside black square

DrawSquare(x,y,r/2,'b')

else % on edge of black square

DrawSquare(x,y,r/2,'m') end

Question 4: (20 points)

Complete the script below to simulate a game in which a person is given two sticks and must select a third from a random bag of sticks until he or she has three sticks that can form a triangle. The lengths of the first two sticks, a and b, are fixed throughout the game. The sticks in the “random bag” have lengths that are uniformly random in (0,1). The game ends when the player can form a triangle with three sticks or after the player has drawn 50 sticks from the bag, whichever happens first. Display the number of sticks the player has drawn from the random bag by the end of the game.

% The two given sticks have lengths a and b. a and b do not change. a= rand(1); b= rand(1);

% Randomly generate c, the length of the third stick, in (0,1). The % simulation ends when it is possible to form a triangle with sticks a,b,c % or after 50 attempts, whichever happens first.

maxTrials= 50;

c= rand(1);

k= 1; % #attempts so far

while (a+b<=c || a+c<=b || b+c<=a) && k<maxTrials

c= rand(1);

k= k+1; end

fprintf('Made %d attempts.\n', k)

Grading notes:

Check that printed k is correct if the first attempt works.

Check that there is never a 51st^ attempt.

% Alternate strategy: % Draw one big light color rectangle and then put dark tiles on top

DrawRect(0,0,floor(right),floor(top),’y’)

for x= 0:2:right-

for y= 0:2:top-

if rem(x,2)==0 && rem(y,2)==0 || ... rem(x,2)==1 && rem(y,2)== % Blue tile when both x and y are even or % when both x and y are odd

DrawRect(x,y,1,1,'b') end end end