






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Practice Exam 1
This exam is open book and open notes. There are 5 problems on the exam worth a total of 100 points. The number of points for each problem is shown below. Try to do something on every problem, and show all of your work.
Good luck!
Name _________________________________
Problem 1 ______________ (15/100 points)
Problem 2 ______________ (20/100 points)
Problem 3 ______________ (20/100 points)
Problem 4 ______________ (20/100 points)
Problem 5 ______________ (25/100 points)
Problem 1: Debugging (20 points)
The code below contains exactly five errors. For each error: (1) circle the error, (2) describe the nature of the error in your own words, and (3) write a modification of the code that “fixes” the error (there are multiple ways to fix each error – just make a change that allows the code to run without generating an error). You do not need to indicate the error message that MATLAB prints. Please keep in mind that you will lose credit for missing an existing error, and for marking something as an error that is not a real error, so make your choices carefully!
n = input('What''s your ''''favorite'''' number? ')
twiceYourFavoritePlusOne = 2n + 1
myFavorites = linspace(2,8,3)
yourFavorites = 2:3:
myFavorites(yourFavorites)
yourFavorites(myFavorites) = 6
myFavoritesSquared = myFavorites * myFavorites
if (rem(n,2) = 0) disp('even') else disp('odd') end
ages4review = [19 7 4 2; 14 89 62; 2 3 2 12 ]
Problem 3: Displaying Plots (20 points)
A MATLAB figure window is shown below. Write MATLAB code that would create the figure exactly as it is shown.
Write your MATLAB code here:
Problem 4: Creating Pictures (20 points)
You are given a MATLAB script called makeBlock.m that creates the 32x32 picture shown below on the left and stores it in a variable called block. You do not need to write the code to create block , you can simply use the block variable. Assume that the block matrix contains the values 0 (black), 0.5 (gray) and 1.0 (white).
Figure 4.1 block
Figure 4.2 bigBlock
Write MATLAB code to produce the 128x128 image (above right), using block. Store the image in a variable called bigBlock. Use comments to explain your strategy as you write your code.
(b) (15 points)
Wendy Wellesley has a theory that computer science professors become easier graders as they get older. She has collected statistics from five introductory courses taught at Wellesley College by five different professors into the table shown below. 1
Course A B C NC Prof’s age CS100 8 7 1 0 55 CS110 25 57 33 2 40 CS111 9 12 4 1 33 CS112 12 2 0 0 55 CS199 3 6 3 0 38
Suppose the following statements are executed to create variables that store this information (note that ages is a column vector):
gradesGiven = [ 8 7 1 0; 25 57 33 2; 9 12 4 1; 12 2 0 0; 3 6 3 0 ];
ages = [55; 40; 33; 55; 38]; courses = {'CS100' 'CS110' 'CS111' 'CS112' 'CS199'}; grades = {'A' 'B' 'C' 'NC'};
(1) In order to test her theory, Wendy would like to calculate the weighted average of grades assigned in each class where an ‘A’ has a weight of 4, a ‘B’ has a weight of 3, a ‘C’ has a weight of 2, and an ‘NC’ has a weight of 0. Write a single MATLAB statement that assigns to the variable classAverages, a column vector containing the weighted average of the grades given in each class.
(2) Write a single MATLAB expression that has the value “true” if and only if all class(es) with the highest grade point average are also the class(es) taught by the oldest professor(s). Your expression may use the variable classAverages assigned in (1).
(^1) These data are, of course, totally factious
CS112 Computation in the Science
Problem 1: Debugging
The following five errors occur in this code, and there are multiple ways to fix each one: (1) twiceYourFavoritePlusOne = 2n + 1 The multiplication operator in the expression 2n is missing – it should be written as 2n. (2) myFavorites(yourFavorites) The variable myFavorites, created by the expression linspace(2,8,3) is assigned to the vector [2 5 8]. The variable yourFavorites, created by the expression 2:3:6 is assigned to the vector [2 5]. When the vector [2 5] is supplied as indices for myFavorites, the index 5 is out of bounds for the myFavorites vector, which has only three elements. The following is one way to fix this error: myFavorites(yourFavorites(1)) (3) myFavoritesSquared = myFavorites * myFavorites This statement uses the matrix multiplication operator, but we cannot multiply two row vectors in this way, so the * operator should be replaced with the element-by-element multiplication operator, . (4) if (rem(n,2) = 0) When comparing two values in a conditional expression, the == operator must be used to test for equality, as in (rem(n,2) == 0). (5) ages4review = [19 7 4 2; 14 89 62; 2 3 2 12 ] The first and third rows of this matrix have 4 elements, whereas the middle row has only 3 elements, so there are inconsistent dimensions in the rows of the matrix. A fourth number should be added to the middle row.
Problem 2: Simulating MATLAB
The following printout is produced by the execution of these statements:
nums = 3 6 9 2 4 6 1 2 3 mySums = 18 12 6 nums = 3 18 9 2 12 6 1 6 3 nums = 3 18 9 0 2 12 20 20 1 6 20 20 0 0 20 20
% copy block as is to mid-upper-right bigBlock(33:64,65:96) = block; % copy block with brightness flipped to upper right corner bigBlock(1:32,97:128) = 1 - block;
Problem 5: Writing MATLAB Code
Part a: The following code statements calculate the requested summation:
leftFactors = 1./(1:100) rightFactors = 1./(2:101) total = sum(leftFactors .* rightFactors)
Part b:
(1) The following code statements show two ways to construct classAverages:
2*gradesGiven(:,3)) ./ sum(gradesGiven,2)
classAverages = (gradesGiven*[4; 3; 2; 0]) ./ sum(gradesGiven,2)
(2) sum((max(classAverages) == classAverages) & (max(ages) == ages)) > 0