L-16 MCS 260 Wed 26 Sep 2001

Exam I on Chapters 1 to 4

  1. Consider the following statements :
           int n = 9535;
           int s = 0;
           while (n > 0)
           {
              s += n % 10;
              n /= 10;
           }
    
    Replace the while with an equivalent do loop :
                                                            +--------+
                                                            |    /10 |
                                                            +--------+
    
  2. Consider the following program :
           #include <stdio.h>
           int main(void)
           {
              int d,l,n = 4395;
              for (l = 0, d = n; d > 0; l++, d /= 10);
              printf("%d\n",l);
              return 0;
           }
    
    1. Rewrite the program into an equivalent one using while instead of a for loop :

    2. In this part we investigate what this program prints. Complete the table below with values for l and d for each step.
            -------------------------
            | step |    d   |   l   |
            =========================
            |   0  |  4395  |   0   |
            |      |        |       |
            |      |        |       |
            |      |        |       |
            |      |        |       |
            |      |        |       |
            |      |        |       |
            -------------------------
      
      So, what does the program finally print?
                                                            +--------+
                                                            |    /30 |
                                                            +--------+
    
  3. An integer number is prime if it is divisible only by 1 and itself. Consider the prototype of the function is_prime :
         int is_prime ( int n );
         /* returns 1 if n is prime, 0 otherwise */
    
    Write the definition of is_prime below :
         int is_prime ( int n )
    
    
                                                            +--------+
                                                            |    /15 |
                                                            +--------+
    
  4. Consider a hat function defined by
        hat(x) = 0    if   x < 1    or  x < -1  
                 1+x  if   x >= -1 and  x < 0 
                 1-x  if   x >= 0  and  x < 1 
    
    The function hat takes a float x as input and returns hat(x) as defined above.

    Give the definition of hat in C :

                                                            +--------+
                                                            |    /15 |
                                                            +--------+
    
  5. Write a program to sum the values of coins.

    The coins are represented by a sequence of characters, like pnqdpqnnppdqddpp.

    Each character stands for a coin:

          -------------------------------
          | character | meaning | value |
          ===============================
          |     p     | penny   | $0.01 |
          |     n     | nickel  | $0.05 |
          |     d     | dime    | $0.10 |
          |     q     | quarter | $0.25 |
          -------------------------------
    
    If the example input pnqdpqnnppdqddpp is put in the file /tmp/input and the program has the name coins, then the program runs like
             [jan@galois]% ./coins < /tmp/input
             value of coins : $1.36
             [jan@galois]% 
    
    Observe that the output value is printed with two digits after the decimal point.
                                                            +--------+
                                                            |    /30 |
                                                            +--------+