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 )
    
                                                            +--------+
                                                            |    /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.
                                                            +--------+
                                                            |    /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 )
    
                                                            +--------+
                                                            |    /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:

    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) */
      
                                                            +--------+
                                                            |    /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.

    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 )
      
                                                            +--------+
                                                            |    /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?
                                                            +--------+
                                                            |    /25 |
                                                            +--------+
    
    FINAL EXAM is in Lecture Center C6 on Monday 3 December 2001 from 1:00 till 3:00PM.