L-34 MCS 260 Wed 7 Nov 2001

Exam II on Chapters 5 to 8

  1. Consider
        int value ( char c );
        /* returns 26, 25, .. , 1 if c equals respectively A, B, .. , Z;
           returns 0 for any character that is not a capital letter  */
    
    Give the definition of the function value :
        int value ( char c )
        {
           if ((c >= 'A') && (c <= 'Z'))
              return 'Z' - c + 1;
           else
              return 0;
        }
    
                                                            +--------+
                                                            |    /10 |
                                                            +--------+
    
  2. Write a program that reads in a sequence of characters and that prints out the same sequence, except that all characters with ASCII code 13 must be replaced by a newline.
         #include<stdio.h>
    
         int main ( void )
         {
            int c;
    
            while ((c = getchar()) != EOF)
               if (c == 13)
                  putchar('\n');
               else
                  putchar(c);
     
            return 0;
         }
    
                                                            +--------+
                                                            |    /15 |
                                                            +--------+
    

  3. Consider
        double value ( int q, int d, int n, int p );
        /* returns the value of the given number of coins, where
              q = number of quarters (value is $0.25 apiece),
              d = number of dimes    (value is $0.10 apiece),
              n = number of nickels  (value is $0.05 apiece), and
              p = number of pennies  (value is $0.01 apiece).    */
    

    Give the definition of the function value :

        double value ( int q, int d, int n, int p )
        {
           double val;
    
           val = q*0.25l + d*0.10l + n*0.05l + p*0.01l;
    
           return val;
        }
    
                                                            +--------+
                                                            |    /10 |
                                                            +--------+
    
  4. The octal number system uses the digits 0,1,2,3,4,5,6,7.

    1. Give the octal representation of the decimal number 54: 66 because 54 % 8 = 6 and 54/8 = 6

    2. Give the definition of the following function
        void write_octal ( int n );
        /* writes the integer n in the octal number system,
           starting with the least significant digit (thus backwards) */
      
        void write_octal ( int n )
        {
           int wn;
      
           for (wn = n; wn > 0; wn /= 8)
              printf("%d", wn % 8);
        }
      
                                                            +--------+
                                                            |    /15 |
                                                            +--------+
    

  5. Variables of an enumeration type day24 take the following values :
           { morning, noon, afternoon, evening, night }.
    
    In a 24 hour day, the morning starts at 6 (= 6AM), noon at 11 (= 11AM), the afternoon at 13 (= 1PM), the evening at 19 (= 7PM), and the night at 21 (= 9PM).

    1. Give the C definition of day24 ensuring that the values of the enumeration type correspond to the respective numbers 6, 11, 13, 19, and 21.
              enum day24 { morning=6, noon=11, afternoon=13, evening=19, night=21 };
      
              typedef   enum day24   day24;
      

    2. Write an input function read for the type day24, using the prototype
        void read ( day24 *dt );
        /* scans a floating point number t, using the convention that 
              for  6 <= t < 11, we call this time of the day morning;
              for 11 <= t < 13, noon; for 13 <= t < 19, afternoon;
              for 19 <= t < 21, evening, and otherwise we call it night. */
      
      Give the definition of read below :
        void read ( day24 *dt )
        {
           float t;
       
           scanf("%f", &t);
      
           if ((t < 6) && (t > 21)) *dt = night;
           else if (t < 11) *dt = morning;
           else if (t < 13) *dt = noon;
           else if (t < 19) *dt = afternoon;
           else *dt = evening;
        }
      
                                                            +--------+
                                                            |    /25 |
                                                            +--------+
    
  6. Consider the following program, using the function R :
         void R ( int *p );
         #include<stdio.h>
         int main(void)                          void R ( int *p )
         {                                       {
            int a = 3;                              static int cnt = 0;
            R(&a);                                  int i = *p * ++cnt;
            R(&a);                                  *p += i;
            printf("%d", a);                     }
            return 0;
         }
    
    Make a diagram representing all variables in main and R. Illustrate with the diagram what happens before, during, and after each call of R(&a). What is printed?
                        variables
    --------------------------------------------------------
                 in main    |            in R
    ========================================================
      before     a          |             cnt
       first     +----+     |              +---+
       call      |  3 |     |              | 0 |
                 +----+     |              +---+
                            |
      during     a          |   p         cnt      i
       first     +----+     |   +-----+    +---+   +----+
       call      |  3 |<--------|- &a |    | 1 |   |  3 |
                 +----+     |   +-----+    +---+   +----+
                            |
      during     a          |   p         cnt      i
      second     +----+     |   +-----+    +---+   +----+
       call      |  6 |<--------|- &a |    | 2 |   | 12 |
                 +----+     |   +-----+    +---+   +----+
                            |
      after      a          |             cnt
      second     +----+     |              +---+
       call      | 18 |     |              | 2 |
                 +----+     |              +---+
                            |
    
      18 is printed
                                                            +--------+
                                                            |    /25 |
                                                            +--------+
    
    FINAL EXAM is in Lecture Center C6 on Monday 3 December 2001 from 1:00 till 3:00PM.