CS 2073-001, Exam 1, Fall 2005
Directions: Fill in answers on the pages below. Don't spend
too much time on any one problem. All questions below ask for a
code segment, which does not need to be a complete program.
You never need any #include statements,
or the main function,
but you should declare the variables that you use.
Points for each problem: 1-15, 2-15, 3-15, 4-20, 5-35.
- Write a single printf statement
that will print the value of an integer n and then the square
of n in exactly the following form (no matter what the value
of n is):
n: [value of n], n squared: [value of n squared]
For example, in case n == 6 it should print:
n: 6, n squared: 36
Similarly, in case n == 11 it should print:
n: 11, n squared: 121
And so forth for any n.
- Write a segment of code that uses an extended if-else
statement to print the proper message based on the value
of a variable IQ, using on the following table.
[You don't have to read a value for IQ.]
| IQ | What to print |
| > 120 | High |
| 90 - 120 | Normal |
| > 0 and < 90 | Low |
| < 0 | Invalid |
- Write a program segment that will use scanf
to read an integer n and then will use a loop
to print the integers from 1 to n
inclusive, with a blank between each one.
Finally print a newline to skip to the next line.
- Insert code into the C program below so that the program
will print the Centigrade temperature
corresponding to an input value for the Fahrenheit temperature.
You need to supply the
prototype and definition of a function
ftoc, which takes a double representing Fahrenheit
as its input parameter and returns the Centigrade temperature using the formula:
C = (5/9)(F - 32).
#include <stdio.h>
/* insert prototype for function ftoc here*/
int main() {
double f, c;
scanf("%lf", &f);
c = ftoc(f);
printf("Fahrenheit: %.2f = Centigrade: %.2f\n", f, c);
}
/* insert definition of ftoc here */
- In this problem you will use a random number generator like the
generator rand_double() that we used in class.
Suppose that it returns a uniformly distributed random
double between 0.0 and 1.0.
(Do not worry about #include statements, or
about initializing the RNG with srand.)
double rand_double() {
return rand()/(double)RAND_MAX;
}
- Write a segment of code which will use this random number
generator and a loop to print 10
random real numbers between 0.0 and 1.0.
- Write a code segment that will use rand_double()
to simulate flipping a coin. Your segment
should print out HEADS half the time and
TAILS half the time.
[Hint: Remember that half the time rand_double() will
return a double that is less than 0.5.]
- Write a segment of code that will use
rand_double() to produce two numbers,
and put these numbers into variables x and
y, where both numbers
are uniformly distributed in the range from
-0.5 to 0.5.
[Hint: Just subtract a constant from the numbers returned by the RNG.]
- Put the previous segment into a loop that will repeatedly
produce two new numbers x and
y and will count those pairs of
numbers generated that satisfy the condition
x2 + y2 < 0.25.
Suppose this count is stored in a
variable count. Your segment should repeat
for N pairs, where N is
1000, say. After N
pairs altogether, the segment should finally print the ratio
count/N as a double. (This ratio is a
monte-carlo approximation to pi/4.)