Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Practice Exam 2 for Computation for the Sciences | CS 112, Exams of Computer Science

Material Type: Exam; Class: Computation for the Sciences; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/17/2009

koofers-user-9fr-1
koofers-user-9fr-1 🇺🇸

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS112 Computation for the Sciences
Practice Exam 2
This exam is open book and open notes. There are 3 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. The times listed below provide rough guidelines
for the amount of time you might spend on each problem.
Good luck!
Name _________________________________
Problem 1 ______________ (55/100 points) (~ 30-35 minutes)
Problem 2 ______________ (15/100 points) (~ 10-15 minutes)
Problem 3 ______________ (30/100 points) (~ 20-25 minutes)
TOTAL ______________
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Practice Exam 2 for Computation for the Sciences | CS 112 and more Exams Computer Science in PDF only on Docsity!

CS112 Computation for the Sciences

Practice Exam 2

This exam is open book and open notes. There are 3 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. The times listed below provide rough guidelines for the amount of time you might spend on each problem.

Good luck!

Name _________________________________

Problem 1 ______________ (55/100 points) (~ 30-35 minutes)

Problem 2 ______________ (15/100 points) (~ 10-15 minutes)

Problem 3 ______________ (30/100 points) (~ 20-25 minutes)

TOTAL ______________

Problem 1: Looping with for and while (55 points)

Part a (10 points): The intent of the following isSorted function is to return true if the numbers contained in the input nums vector are arranged in increasing order, and false otherwise. For example, the expression isSorted([2 5 6 8]) should return true, while isSorted([3 2 6 7]) should return false. Unfortunately, the function definition is buggy. Describe the bugs and modify the code so that it works correctly. Note: MATLAB has built-in functions true and false that return the logical values 1 and 0, respectively.

function answer = isSorted(nums) for i = 1:length(nums) if (nums(i) < nums(i-1)) answer = false; break; end end

Part b (15 points): Rewrite the isSorted function using a while loop in place of the for loop. In your new definition, the loop should also terminate if a pair of adjacent locations are found that contain values that are in decreasing order, but you cannot use a break statement to terminate the loop.

Part d (15 points): Write a function sumMX that has a single input that is a 2-D matrix that is assumed to contain all 0’s and 1’s, and a single output that is a 2-D matrix of the same size. This function should count the number of 1’s contained in a 3x3 block centered at each location of the input matrix, and store this sum at the corresponding location of the output matrix. You do not need to calculate these sums for the locations around the border of the image – the output matrix can contain zeros at these locations. Given the following input matrix:

1 0 1 1 0 0 1 0 1 1 1 0 0 1 0 0 1 0 0 1 1 1 1 0 1

The sumMX function should return the following output matrix:

0 0 0 0 0 0 4 5 5 0 0 3 4 4 0 0 5 4 4 0 0 0 0 0 0

Problem 2: Function inputs and outputs (15 points)

A student has written the following functions (using very poor choices of variables and no comments) in an M-file named myFunction.m.

function [a b c] = myFunction(a, b, c) [c b a] = mySubFunction(a,b,c);

function [a b c] = mySubFunction(x, y, z) nums = [x y z]; a = max(nums); c = min(nums); b = nums((nums ~= a) & (nums ~= c));

What are the values returned in the variables a,b, and c when the following command is typed into MATLAB’s Command Window?

[a b c] = myFunction(2,1,3);

CS112 Computation for the Sciences

Exam 2 Solutions

Problem 1: Looping with for and while

Part a: There are two bugs in the original isSorted function. First, the index i begins at the value 1 in the for loop, causing the expression nums(i-1) to generate an error message because the expression i-1 evaluates to 0, which is not a valid index. Second, in the case where the numbers in the input nums vector are arranged in increasing order, the output parameter answer is never assigned to a true value. The following definition fixes both of these problems:

function answer = isSorted(nums) answer = true; for i = 2:length(nums) if (nums(i) < nums(i-1)) answer = false; break; end end

Part b: The following definition of isSorted uses a while loop in place of the for loop, and terminates the loop if a pair of adjacent locations are found that contain values that are in decreasing order, without using a break statement:

function answer = isSorted(nums) answer = true; i = 2; while (answer & (i <= length(nums)) if (nums(i) < nums(i-1)) answer = false; end i = i + 1; end

Part c: The function mystery sorts the numbers contained in the input nums vector in increasing order, using a sorting strategy known as selection sort. Suppose there are n numbers in the input vector. The selection sort algorithm first places the maximum value into the last location (index n) of the vector. Then the second largest value is placed at index n-1, the third largest value is placed at index n-2, and so on. When a number is moved from an index i to its final place at the index given by index, the contents at index are moved down to index i. When mystery is called with the vector [8 2 7 3 6], the contents of the nums vector after each iteration of the for loop are as follows:

initial values: [8 2 7 3 6] i = 5: [6 2 7 3 8] i = 4: [6 2 3 7 8] i = 3: [3 2 6 7 8] i = 2: [2 3 6 7 8]

This results in the following printout from the specified commands:

nums = [8 2 7 3 6]; newNumbers = mystery(nums) newNumbers = 2 3 6 7 8 nums nums = 8 2 7 3 6

Part d: The following is one possible definition of sumMX:

function outMX = sumMX(inMX) [rows cols] = size(inMX); outMX = zeros(rows, cols) for row = 2:rows- for col = 2:cols- outMX(row, col) = sum(sum(inMX(row-1:row+1, col-1:col+1))); end end

Problem 2: Function inputs and outputs

The following printout shows the values returned in the variables a, b, and c when the following command is typed into MATLAB’s Command Window:

[a b c] = myFunction(2,1,3) a = 1 b = 2 c = 3