2013F002/Midterm
From CDS 130
Midterm Exam for CDS 130, Fall 2013
 The total is 100 points.
 Write your answers on the provided blank booklet.
 This is a closedbook and closednote exam. No use of MATLAB is allowed.
 You have until the end of the class period to finish.
 Any student caught cheating will receive a zero on the exam and will be reported to the Honor Council.
 Calculators are allowed (but no smartphones). If you do not have a calculator, write down the operation that you would need to do in order to get the answer, e.g., 2^{22} or
100000/7
.  Show your work or write down your thoughts for partial credit. When we grade exams, we look for a correct answer first. If the answer is not correct, we look for evidence of understanding; the more understanding that is demonstrated, the more partial credit.
1. Decimaltobinary (5 pts)
Convert the decimal number 103
to binary
Answer 


2. Exponents (5 pts)
You are in a room with a panel of 8 light switches that can be either up or down. How many possible configurations are there? For example, one configuration is up, up, down, up, up, up, down, up.
Answer 

2^{8}=256 
A hexidecimal "digit" may be any one of the following 16 symbols: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
. How many unique patterns composed of two hexideximal digits can be formed? For example, one pattern is 00
, and another pattern could be FF
.
Answer 

16^{2}=256
Here you have two boxes that can be filled with one of 16 symbols. There are 16 patterns that start with 
3. Logic Gates (10 pts)
Below are two AND, two NOT, and an OR gate connected.
The logic table for the NOT gate is given. 
Answer  

The table for the NOT gate indicates that it outputs zero when the input is 1 and outputs 1 when the input is zero.  Input A  Input B  Output 
0  0  0  
0  1  1  
1  0  1  
1  1  0 
4. Matrix Syntax (10 pts)
Write a set of commands that will create the following matrix.
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
Answer 

M = [1:6]; M = [M;M;M;M;M;M;M;M;M;M]; or M = [1:6]; M = [M;M;M;M;M]; M = [M;M]; or for i = [10:1:1] % [1:10] will also work for j = [1:6] M(i,j) = j; end end or for j = [10:1:1] % [1:10] will also work for i = [1:6] M(j,i) = i; end end 
5. Iteration (10 pts)
Suppose that you named the matrix in the previous question M
. Write a for
loop that could be placed after the program that you wrote for your answer for the previous question that will set all of the numbers in the first row equal to the column that the number is in.
Answer 

Many students noted that the matrix in the previous answer already satisfied the requirement above and so no additional code was needed. This is correct. The problem statement was intended to read "will reset all of the numbers ...". for j = [1:6] M(1,j) = j; end or i = 1; for j = [1:6] M(i,j) = j; end or (this resets everything) for i = [1:10] for j = [1:6] M(i,j) = j; end end or (this resets only first row, but will take longer to run) for i = [1:10] for j = [1:6] if (i ==1) M(i,j) = j; end end end 
6. Interpreting a loop (10 pts)
Which matrix will result from evaluating the nested for loop?

counter = 0; for j = [3:1:1] for i = [1:3] M(i,j) = counter; counter = counter+1; end end M counter 
A. M = 8 7 6 5 4 3 2 1 0 
B. M = 1 2 3 2 4 6 3 6 9 
C. M = 6 3 0 7 4 1 8 5 2 
D. M = 2 3 4 3 4 5 4 5 6 
Answer  

The answer is C. To see this, write out the commands in longhand. If any answer but C. was chosen and there was work on the page, 5 points were given with one exception: If they wrote something equivalent to "None of the above because counter is not displayed; if it were displayed the answer would be C they received significant partial credit. However, the question does not ask what is displayed. It only asks about the resulting matrix.

7. Nested for loops (10 pts)
for j = [1:10] for i = [1:10] B(j,i) = i+j; end end 
2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4

3 2 1 3 2 1 3 2 1

1 0 0 0 4 0 0 0 9

8. Double for Loop (5 pts)
If you typed clear; for j = [1:3] for i = [1:j] M(i,j) = i; end end 

Answer 

>>M(3,3) ans = 3 and >>M(3,2) ans = 0 
9. Nested For Loop 3 (10)
B = 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
Write a program that creates this matrix using nested for loops. 
Answer 

B(4,6) = 0; count = 25; for i = [1:4] for j = [1:6] count = count  1; B(i,j) = count; end end B 
10. Array Boundaries (5 pts)
When the following program is entered, clear; A = [1,2,10,12]; for i = [1:2:11] b = A(i+1) end 
the result is ??? Index exceeds matrix dimensions. Explain the reason for the error. Modify the above program so that it will execute without error. 
Answer 

The longhand version of this program is i = 1; b = A(i+1) i = 3; b = A(i+1); i = 4; b = A(i+1); On the last line, you are assigning the value in i = [1:2:11] to i = [1:2:3] or, change the line A = [1,2,10,12]; to make A have 12 elements. For example, A = [1,2,10,12,13,14,15,16,17,18,19,20]; 
11. Interpreting an image (5 pts)
To produce the image shown, all of the lines inside the nested for loop should be removed except for two lines. Which two lines should be kept?
clear; M(10,10) = 1; C = [1 1 1;0 0 0]; for i = [1:10] for j = [1:10] M(i+1,i) = 1; % Option A. M(i+4,j) = 1; % Option A. M(1,10) = 1; % Option B. M(4,j) = 1; % Option B. M(i,10) = 1; % Option C. M(6,j) = 1; % Option C. M(i,10) = 1; % Option D. M(i,4) = 1; % Option D. end end colormap(C); imagesc(M); colorbar; 

12. Colormap (10 pts)
Modify the color matrix C
and the matrix values in matrix M
in the following program so that when the following commands are entered the image shown is produced.
clear;clc M = [1 0 1 0 1; 0 1 0 1 0; 1 0 1 0 1; 0 1 0 1 0; 1 0 1 0 1]; C = [0 0 0; 1 0 0]; colormap(C) imagesc(M) axis square

13. Replacement in an array (5 pts)
Given an array with 7
elements that are either 0
or 1
, write a program that implements the algorithm
Replace any0
with a1
, and any1
with a0
, but do not modify the first and last values in the array.
Answer 

% Solution should work for any combination of 1s and 0s in this array. clear; A = [0,0,0,1,1,0,0] for i = [2:6] if (A(i) == 0) A(i) = 1; else A(i) = 0; end end A 