2013F002/Midterm

From CDS 130

Jump to: navigation, search

Mid-term Exam for CDS 130, Fall 2013

  1. The total is 100 points.
  2. Write your answers on the provided blank booklet.
  3. This is a closed-book and closed-note exam. No use of MATLAB is allowed.
  4. You have until the end of the class period to finish.
  5. Any student caught cheating will receive a zero on the exam and will be reported to the Honor Council.
  6. 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., 222 or 100000/7.
  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.

Contents

  1. Decimal-to-binary (5 pts)
  2. Exponents (5 pts)
  3. Logic Gates (10 pts)
  4. Matrix Syntax (10 pts)
  5. Iteration (10 pts)
  6. Interpreting a loop (10 pts)
  7. Nested for loops (10 pts)
  8. Double for Loop (5 pts)
  9. Nested For Loop 3 (10)
  10. Array Boundaries (5 pts)
  11. Interpreting an image (5 pts)
  12. Colormap (10 pts)
  13. Replacement in an array (5 pts)

1. Decimal-to-binary (5 pts)

Convert the decimal number 103 to binary

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.

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.

3. Logic Gates (10 pts)

Below are two AND, two NOT, and an OR gate connected.

  1. Generate a table with a columns labeled A, B, OUTPUT.
  2. For each possible combinations of A and B, enter the corresponding value for OUTPUT.

The logic table for the NOT gate is given.

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

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.

6. Interpreting a loop (10 pts)

Which matrix will result from evaluating the nested for loop?

  • A.
  • B.
  • C.
  • D.
  • All of the above.
  • None of the above.
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

7. Nested for loops (10 pts)

  • Original program
for j = [1:10]

  for i = [1:10]
    B(j,i) = i+j;  
  end

end
  • Modify the program to the left so that it creates a matrix B with values of
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
  • Modify the program to the left so that it creates a matrix B with values of
-3 -2 -1
-3 -2 -1
-3 -2 -1
  • Extra credit: Modify the program to the left so that it creates a matrix B with values of
 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
  1. and then entered M(3,3) on the command line and then hit enter, what do you expect to see displayed on the screen?
  2. if you then entered M(3,2) on the command line and hit enter, what do you expect to see displayed on the screen?

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.

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.

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?

  • The two lines with % Option A. at the end
  • The two lines with % Option B. at the end
  • The two lines with % Option C. at the end
  • The two lines with % Option D. at the end
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 any 0 with a 1, and any 1 with a 0, but do not modify the first and last values in the array.
Personal tools