CS 2073-001,   Final Exam, Fall 2005
Answers

Directions: Fill in answers on the pages below. Don't spend too much time on any one problem.
Points for each problem: 1-15, 2-15, 3-20, 4-20, 5-25, 6-20, 7-20, 8-25.

Answers are in red below.


  1. if-else: Suppose a variable temperature is supposed to be in the range from 96.0 to 100.6 inclusive. Write a short piece of C code (not an entire program or even a function) that will print the message "Temperature OK" in case the temperature is in range and will print "You are sick" in case the temperature is out of range.


  2. loops: Rewrite the following for loop as a while loop that will print the same thing.


  3. Arrays: Given the two arrays a and b below, use a loop to let c be the sum of a and b, that is, each element of c is the sum of the corresponding elements of a and b. Then print the elements of c. (You must use loops for both parts.)


  4. Arrays as parameters: Write a function average with parameters a 1-dimensional array of integers and the size of the array. This function should find the average of the array elements (sum of the elements divided by the number of them). average should return this number as a double. For example, the main function below should print 90.0. Your function must use a loop and must work for an array of any size greater than 0.


  5. Series: Write a function series that will calculate the sum of n terms of the following series:

    Notice that the signs are alternating. Your function should have the integer n as a formal parameter, and it should then use a loop to calculate the sum and to return that as the answer. For example, the call series(2) should return the number 1 - 1/22 = 1 - 0.25 = 0.75.


  6. Pointers:
    1. What will the program below print? Without any changes, it will print 2005.

    2. Use pointers and make alterations so that the program below will print 2006. (Do not introduce any new variable names.)


  7. structs: Use the definition of a struct in the code below.


  8. Everything: Consider the following complete program: Note: in the program below, the field "char suit" should have been "int suit", although it works as it is.

    Deck of cards Output: 1st part Output: 2nd part
    /*  1 */  #include <stdlib.h>
    /*  2 */  #include <time.h>
    /*  3 */
    /*  4 */  struct card { /* single card */
    /*  5 */     char suit; /* suit */
    /*  6 */     int value; /* value (spots) */
    /*  7 */  };
    /*  8 */  void printdeck(struct card deck[]);
    /*  9 */
    /* 10 */  int main() {
    /* 11 */     // deck of 52 cards
    /* 12 */     struct card deck[52];
    /* 13 */     struct card temps;
    /* 14 */
    /* 15 */     double ran[52];
    /* 16 */     int i, dum;
    /* 17 */     
    /* 18 */
    /* 19 */     // initialize deck
    /* 20 */     for (i = 0; i < 52; i++) {
    /* 21 */        deck[i].suit = i/13;
    /* 22 */        deck[i].value = i%13 + 1;
    /* 23 */     }
    /* 24 */
    /* 25 */     // print the deck
    /* 26 */     printdeck(deck);
    /* 27 */
    /* 28 */     srand((long)time(NULL));
    /* 29 */     for (i = 0; i < 52; i++)
    /* 30 */        ran[i] = rand()/(double)RAND_MAX;
    /* 31 */
    /* 32 */     for (dum = 0; dum < 51; dum++)
    /* 33 */        for (i = 0; i < 51; i++)
    /* 34 */           if (ran[i] < ran[i+1]) {
    /* 35 */              // interchange ran array elts
    /* 36 */              double temp = ran[i];
    /* 37 */              ran[i] = ran[i+1];
    /* 38 */              ran[i+1] = temp;
    /* 39 */
    /* 40 */              // interchange deck array elts
    /* 41 */              temps = deck[i];
    /* 42 */              deck[i] = deck[i+1];
    /* 43 */              deck[i+1] = temps;
    /* 44 */           }
    /* 45 */
    /* 46 */     // print the deck
    /* 47 */     printdeck(deck);
    /* 48 */  }
    /* 49 */
    /* 50 */  void printdeck(struct card deck[]) {
    /* 51 */     int i, v, s;
    /* 52 */     printf("Deck of cards:\n");
    /* 53 */     for(i = 0; i < 52; i++) {
    /* 54 */        v = deck[i].value;
    /* 55 */        if (v == 1) printf(" A");
    /* 56 */        else if (v == 13) printf(" K");
    /* 57 */        else if (v == 12) printf(" Q");
    /* 58 */        else if (v == 11) printf(" J");
    /* 59 */        else printf("%2i", deck[i].value);
    /* 60 */        printf(" of ");
    /* 61 */        s = deck[i].suit;
    /* 62 */        if (s == 0) printf("Spades");
    /* 63 */        else if (s == 1) printf("Hearts");
    /* 64 */        else if (s == 2) printf("Clubs");
    /* 65 */        else if (s == 3) printf("Diamonds");
    /* 66 */        printf("\n");
    /* 67 */     }
    /* 68 */     printf("\n");
    /* 69 */  }
    
    Deck of cards:
     A of Spades
     2 of Spades
     3 of Spades
     4 of Spades
     5 of Spades
     6 of Spades
     7 of Spades
     8 of Spades
     9 of Spades
    10 of Spades
     J of Spades
     Q of Spades
     K of Spades
     A of Hearts
     2 of Hearts
     3 of Hearts
     4 of Hearts
     5 of Hearts
     6 of Hearts
     7 of Hearts
     8 of Hearts
     9 of Hearts
    10 of Hearts
     J of Hearts
     Q of Hearts
     K of Hearts
     A of Clubs
     2 of Clubs
     3 of Clubs
     4 of Clubs
     5 of Clubs
     6 of Clubs
     7 of Clubs
     8 of Clubs
     9 of Clubs
    10 of Clubs
     J of Clubs
     Q of Clubs
     K of Clubs
     A of Diamonds
     2 of Diamonds
     3 of Diamonds
     4 of Diamonds
     5 of Diamonds
     6 of Diamonds
     7 of Diamonds
     8 of Diamonds
     9 of Diamonds
    10 of Diamonds
     J of Diamonds
     Q of Diamonds
     K of Diamonds
     
    Deck of cards:
     5 of Diamonds
     6 of Diamonds
     2 of Hearts
     8 of Diamonds
     2 of Clubs
     J of Spades
     2 of Diamonds
    10 of Hearts
     J of Diamonds
     6 of Hearts
     Q of Hearts
     K of Diamonds
     7 of Spades
     A of Clubs
     8 of Hearts
     7 of Diamonds
     K of Hearts
     8 of Clubs
     K of Spades
     J of Hearts
     9 of Clubs
     4 of Hearts
     6 of Spades
     8 of Spades
     7 of Hearts
    10 of Spades
    10 of Diamonds
     5 of Spades
    10 of Clubs
     A of Hearts
     3 of Clubs
     9 of Diamonds
     6 of Clubs
     2 of Spades
     4 of Diamonds
     3 of Spades
     9 of Spades
     A of Spades
     J of Clubs
     3 of Diamonds
     Q of Clubs
     7 of Clubs
     3 of Hearts
     9 of Hearts
     4 of Spades
     Q of Diamonds
     4 of Clubs
     Q of Spades
     5 of Hearts
     K of Clubs
     A of Diamonds
     5 of Clubs
    

    The output shown is not the entire output of this program. Even if you didn't study or see this example, you still should be able to answer some of the questions below:

    1. What is line 15 doing? It is declaring an array named ran of doubles of size 52 (elements indexed from 0 to 51 inclusive).

    2. What are lines 28-30 doing? Line 28 initializes the RNG so that it will give different numbers each time it is run. Lines 29-30 insert a random double uniformly distributed between 0 and 1 into each location in the array ran.

    3. What different integers are stored in the value field of the struct? Integers from 1 to 13 (inclusive) are stored in this field.

    4. What different integers are stored in the suit field of the struct? Integers from 0 to 3 (inclusive) are stored in this field. (Originally, I meant to have a char here, either 'S', 'H', 'D', or 'C', for the four suits in cards. Then I changed my mind, but forgot to change the type from char to int. However, a char works like an int, so the program works as it is.)

    5. What are lines 32-44 doing? These lines are sorting the array ran into decreasing order by making a number of interchanges of adjacent array elements. But every time a pair of numbers from the ran array is interchanged, this part of the code interchanges the same pair in the deck array.

    6. What will the rest of the output look like? (The "second part" of the output that is not shown.) This output results from the second call to printdeck, at line 47. This will be the same 52 "cards", but they will be in random order.

    7. If we run the program again, will the second part of the output look the same? (More than just yes or no) Because the srand call on line 28 uses the time in seconds to initialize the RNG, the random numbers provided (and the resulting order of the deck array) will be completely different from one run to the next, unless the two runs are very close together in time.

    8. Explain briefly how this program manages to get the final output that it does (the second part of the output that is not shown). Initially the ran array is in random order. When we sort it into decreasing order, we do the same operations to the deck array, which randomizes it.