CS 2073-001,   Exam 1, Spring 2006
Answers

Note: Answers are in red.
Points for each problem: 1-10, 2-15, 3-15, 4-15, 5-15, 6(i)-15, 6(ii)-15.


  1. Write a single printf statement that will print the value of a positive integer n and then the square root of n with labels so that it looks exactly as follows (with 8 digits to the right of the decimal place):


  2. Suppose a temperature is stored in a double variable temp. For humans, if their body temperature is between 97.6 and 100.3 inclusive, this is NORMAL. Bigger than or equal to 107 or less than or equal to 95 is LIFE-THREATENING. Between 100.3 and 107 is HIGH and between 95 and 97.6 is LOW. Write some sort of if-else statement (perhaps extended or complex) that will print the correct term based on the value of temp. [Do not read a value for temp, but assume is is given.]


  3. Write a program segment that will use scanf to read a positive integer n and then will use a loop to print the squares of integers from 1 to n inclusive, on separate lines. Thus if you read a value of 4, your segment should print:


  4. The surface area of a cylinder with height h and radius r (including the top and bottom) is given by the formula:

    Add code to the program below, so you define a function named area, with two formal parameters h and r, that will calculate and return the area. You also need to give a prototype.


  5. Insert code into the C program below so that the program will print a triangle of start of size n for whatever positive value n has;


  6. For this problem, you are to make use of the following function roll() that returns a random integer between 1 and 6 inclusive each time it is called. Thus each of the six integers is equally likely to be returned by the function.

    (In the questions below do not worry about #include statements, or about initializing the RNG with srand.)

    1. Write a segment of code which will use this function and a loop to print 30 random integers between 1 and 6. Print them all on one line with blanks between them.

        #include <stdio.h>
        #include <stdlib.h>
        
        int main() {
           int i;
           for (i = 0; i < 30; i++)
              printf("%i ", roll());
        
           printf("\n");
        
        }
        
        int roll() {
           return (int)(6.0*rand()/(double)RAND_MAX + 1.0);
        }
        One run:
        4 2 2 4 6 2 5 2 3 1 1 3 2 3 6 4 5 4 5 5 5 1 4 2 3 6 4 3 4 5
        

    2. Write a code segment that will use this function and a loop to repeatedly simulate rolling a die until a 6 comes up. Your program should count the number of rolls needed to get to a 6, and print the final count.

        #include <stdio.h>
        #include <stdlib.h>
        #include <time.h>
        
        int main() {
           int d;
           int count = 0;
           srand((long)time(NULL)); /* initialize rand() */
           while (1) {
              d = roll();
              count++;
              if (d == 6) break;
           }
           printf("Rolls to get a 6: %i\n", count);
        }
        
        int roll() {
           return (int)(6.0*rand()/(double)RAND_MAX + 1.0);
        }
        Multiple runs:
        Rolls to get a 6: 1
        Rolls to get a 6: 9
        Rolls to get a 6: 22
        Rolls to get a 6: 6
        Rolls to get a 6: 2
        Rolls to get a 6: 2
        Rolls to get a 6: 19
        Rolls to get a 6: 11
        Rolls to get a 6: 4
        Rolls to get a 6: 2
        Rolls to get a 6: 2
        Rolls to get a 6: 5
        Rolls to get a 6: 10
        Rolls to get a 6: 9
        Rolls to get a 6: 7
        Rolls to get a 6: 2