



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 7
This page cannot be seen from the preview
Don't miss anything!




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.
x
y
x
y
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.
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
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