2013F002/MidtermSample1

From CDS 130

Jump to: navigation, search

Sample Mid-term 1

Contents

  1. Binary-to-decimal
  2. Decimal-to-binary
  3. Bytes per dollar
  4. Logic Gates
  5. Array Syntax
  6. Matrix Syntax
  7. Iteration
  8. Interpreting a for loop
  9. Nested for loops
  10. Colormap I
  11. Colormap II
  12. Colormap III
  13. Replacement in an array
  14. Replacement in a matrix

1. Binary-to-decimal

Convert 00111 to decimal

2. Decimal-to-binary

Convert 49 to binary

3. Bytes per dollar

If a 300 GB hard drive costs $100 dollars and a 4.7 GB DVD disk costs $2 dollars, which is a better deal in terms of bytes per dollar? Show your work.







4. Logic Gates

In the image below, four NANDS are connected and three of the inputs are set to 1. What are the values ofZ and A if B = 1 and Output = 1? For reference, the logic table associated with a NAND gate is shown.

  1. Z = 1, A = 0
  2. Z = 0, A = 0
  3. Z = 1, A = 1
  4. Z = 0, A = 1

Image:NAND_LOGIC_GATE.svg
Input A Input B Output
0 0 1
0 1 1
1 0 1
1 1 0

5. Array Syntax

Which of the following (circle each one that applies)

  • A = [-1:3];
  • A = [-1:2:3];
  • A = [3:-1:-1];
  • A = [-1,0,1,2,3];
  • A = [-1,1,3];
  • A = [-1,[1,3]];
  • A = [-1,[1:2:3]];

is the same as entering

A(1) = -1;
A(2) = 1;
A(3) = 3;

6. Matrix Syntax

Write a set of commands that will create the following matrix

-1 -2 -3
-1 -2 -3
 1  3  3

7. Iteration

clear;
A(9) = 13;
 for i = [10,11,12]
  A(i) = A(i-1)+37;
end

What is A(12) and A(1)?

8. Interpreting a for loop

Which, if any, nested for loops will produce the matrix M?

  • A.
  • B.
  • C.
  • D.
  • All of the above.
  • None of the above.
 M =
 1 1 1
 2 2 2
 3 3 3

A.

for j = [1:3]
  for i = [1:3]
    M(i,j) = i;
  end
end

B.

for i = [1:3]
  for j = [1:3]
    M(i,j) = i;
  end
end

C.

for i = [0:2]
  for j = [0:2]
    M(i+1,j+1) = i+1;
  end
end

D.

for j = [0:2]
  for i = [0:2]
    M(i+1,j+1) = i+1;
  end
end


9. Nested for loops

  • Original program


for i = [1:3]
  for j = [1:3]
    Z(i,j) = 1.0;
  end
end
  • Modify the program to the left so that it creates a matrix Z with values of
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
  • Modify the program to the left so that it creates a matrix Z with values of
1 2 3
1 2 3
1 2 3
  • Extra credit: Modify the program to the left so that it creates a matrix Z with values of
1 2 3
2 4 6
3 6 9

10. Colormap I

Suppose that you have the following colormap matrix:

C = [1.0, 0.0, 0.0 ; 0.0, 1.0, 0.0 ; 0.0, 0.0, 1.0];

Which commands could be used to remove the color green and replace it with the color represented by mixing128 drops of Red, Green, and Blue? Circle the best answer.

A.

C(2,1) = 128;
C(2,2) = 128;
C(2,3) = 128;

B.

C(2,1) = 0.50;
C(2,2) = 0.50;
C(2,3) = 0.50;

C.

C(1,1) = 0.50;
C(2,1) = 0.50;
C(3,1) = 0.50;

D.

C(1,1) = 128;
C(2,1) = 128;
C(3,1) = 128;


11. Colormap II

Suppose that you entered the following commands:

clear;
counter = 1;
for i = [1:10]
  for j = [1:10]
    M(i,j) = counter;
    counter = counter+1;
  end
end

for j = [1:100]
  C(j,1) = j/100;
  C(j,2) = 0;
  C(j,3) = 0;
end

imagesc(M)
colormap(C)

Which is the correct image?

  • A
  • B
  • C
  • D
A
A
B
B
C
C
D
D


12. Colormap III

clear; 
C = [1 1 1;0 0 0];
 for i = [1:10]
 for j = [1:10]
 
 M(i,i) = 1; % Option A.
 
 M(i,j) = 1; % Option B.
 
 M(i,1) = 1; % Option C.
 M(i,2) = 1; % Option C.
 
 M(1,j) = 1; % Option D.
 M(2,j) = 1; % Option D.
 
 end
 end
 imagesc(M)
 colormap(C)
 axis square;
 colorbar;

To produce the image shown, which line or lines should be kept in the program?

  • The single line with % Option A. shown
  • The single line with % Option B. shown
  • The two lines with % Option C. shown
  • The two lines with % Option D. shown


13. Replacement in an array

Given an array with 5 elements that are either 0 or 1, write a program that implements the algorithm

Replace any 0 with a 1.


14. Replacement in a matrix

Given a matrix with 5 rows and 5 columns, with elements that are either 0 or1, write a program that implements the algorithm

Replace any 0 with a 1.


Personal tools