ECE 190 Midterm Exam 1 - National Center for … · · 2011-02-08ECE 190 Midterm Exam 1 Spring...
Click here to load reader
Transcript of ECE 190 Midterm Exam 1 - National Center for … · · 2011-02-08ECE 190 Midterm Exam 1 Spring...
ECE 190 Midterm Exam 1 Spring 2011
Practice exam, February 2011
Name: NetID:
Programming studio section (mark only the one you attend):
DCL 440 DCL 520
9:00 AM [ ] AD4 [ ] AD6
12:00 PM [ ] AD5 [ ] AD8
1:00 PM [ ] AD3 [ ] AD7
2:00 PM [ ] AD9
3:00 PM [ ] AD1 [ ] AD2
Be sure your exam booklet has 5 pages.
Do not tear the exam booklet apart.
Write your name at the top of each page.
This is a closed book exam.
You may not use a calculator.
You are allowed one handwritten 8.5 x 11" sheet of notes.
Absolutely no interaction between students is allowed.
Be sure to clearly indicate any assumptions that you make.
Don’t panic, and good luck!
Problem 1 10 points _______________________________
Problem 2 5 points _______________________________
Problem 3 10 points _______________________________
Problem 4 5 points _______________________________
Total 30 points _______________________________
Page: 2 Name: ___________________________________________
Problem 1 (10 points): Binary representation
Part A (2 points): What range of decimal numbers can be represented using 9 bits in each 2’s
complement, 1’s complement, and unsigned binary representations?
2’s complement: _____________________
1’s complement: _____________________
Unsigned binary: _____________________
Part B (2 points): Base 8 representation of numbers is called “octal”, and uses only digits 0
through 7. Give the following equivalent numbers for octal number 1275 in each binary,
decimal, and hexadecimal representation.
Binary: _____________________
Decimal: _____________________
Hexadecimal: _____________________
Part C (1 point): How is the number 2
represented in the floating point data type format?
Answer: _________________________________________________
Part D (1 points): What is the largest possible positive number that can be expressed in the
floating point data type, using an exponent between 1 and 254? Write your answer in binary, in
IEEE 754 floating point format.
Answer: _________________________________________________
Part E (4 points): Using 2-complement arithmetic, what 32-bit number can be added to this
floating point number to effectively multiply by 28?
0 01111101 00000000000000000000000
Answer: _________________________________________________
Page: 3 Name: ___________________________________________
Problem 2 (5 points): Digital logic
Part A (3 points): Using NAND gates only, draw circuits that implement AND and OR gates
AND
OR
Part B (1 points): Using exactly one logic gate whose inputs are not inverted, draw a circuit
equivalent to the given circuit
Part C (1 points): One way to think of logic gate types is to consider what input states guarantee
a certain output state. For example, we could describe the function of an AND gate as “Any low
input guarantees a low output.” Identify what type of gate is represented by the following
phrase: “Any low input guarantees a high output.”
Answer: ____________________________
Page: 4 Name: ___________________________________________
Problem 3 (10 points): Memory Given four 1-bit memory cells, build a 2x2-bit memory unit using only AND, OR, NOT gates
and MUXes:
Page: 5 Name: ___________________________________________
Problem 4 (5 points): C language
Part A (1 points): Assume variables x, y, and z are already declared. Write a C expression to
evaluate 𝑥 + 1
1 + 𝑦𝑧5
Do not switch the order of operands.
Answer: ___________________________________________________
Part B (2 points): Fill in missing expressions to implement the following formula:
𝑓 = {𝑥 𝑓𝑜𝑟 𝑥 ≤ 100 𝑥 𝑓𝑜𝑟 10 < 𝑥 ≤ 100𝑥 + 1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
if ( _________________________________________ )
f = x;
else if ( ____________________________________ )
f = -x;
else
f = x + 1;
Part C (1 points): Write the output produced by the following code segment:
int x = 5;
do {
printf(“%d “, --x);
} while (x != 4)
Answer: ____________________________________________
Part D (1 points): Using only declared variables, write missing arguments in printf function
call that correspond to the provided format conversion specifications.
int count;
char inchar;
printf(“%c %d”, _________________________________________ );
Page: 6 Name: ___________________________________________
Programming Problem
In this assignment, you will need to write a program to calculate π by a Monte Carlo method. The idea of
the Monte Carlo method for computing π is to throw darts at a 2D board. As such, each dart will have an
x coordinate and a y coordinate of the point where it lands. You will then calculate what fraction of darts
falls within a circle with center (0,0) and radius 1. Remember that the point (x,y) is distance √𝑥 + 𝑦
from the origin (0,0). If you throw 100+ darts in this manner, the fraction of darts hitting the unit circle
should start to approximate π/4≈0.785398163.
Your program will first request how many darts you will throw. It will then loop through each dart,
prompting "x and y for dart i? " where i is the number of the current dart, starting from 0. Finally, your
program should output "You hit the unit circle j times." followed by a newline, where j is the number of
darts that landed in the circle.
Here is a sample run:
How many darts? 10
x and y for dart 0? -0.233169 -0.866316
x and y for dart 1? -0.999984 -0.736924
x and y for dart 2? 0.511211 -0.082700
x and y for dart 3? 0.065534 -0.562082
x and y for dart 4? -0.905911 0.357729
x and y for dart 5? 0.358593 0.869386
x and y for dart 6? -0.232996 0.038833
x and y for dart 7? 0.661931 -0.930856
x and y for dart 8? -0.893077 0.059400
x and y for dart 9? 0.342299 -0.984604
You hit the unit circle 7 times
You may NOT use any functions from math.h, such as sqrt.
NOTE: For testing purposes, you can generate x and y coordinates on the interval [-1, 1] by writing at the
top of your program
#include <stdlib.h>
#define FRAND (rand()/(RAND_MAX/2.0)-1.0)
and using "x = FRAND;" to obtain a new random number to assign to x instead of using scanf to
obtain this value form user. Do not forget to remove this test code when turning in your final program!
Name your program file exam1.c. You should compile your code as follows:
gcc -g -Wall -Werror -ansi exam1.c -o test
Make sure your program compiles and works before your turn it in. We will not grade a program that
does not compile and you will lose points if the program compiles with warnings. You obviously will not
receive any points if your program does not do what it is supposed to do. Both functionality and coding
style will be graded, with most points given for functionality and some points given for style. No style
grading will be done if the program does not compile. To turn in the program, execute
handin --EX 1 exam1.c