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 :
    
            int n = 9535;
    	int s = 0;
            do
    	{
               s += n % 10;
               n /= 10;
    	} while (n > 0);
    
    
                                                            +--------+
                                                            |    /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 :
      	#include <stdio.h>
      	int main(void)
      	{
      	  int d,l,n = 4395;
      
                l = 0;
                d = n;
                while (d > 0)
                {
                   l++;
                   d /= 10;
                }
      
      	  printf("%d\n",l);
      	  return 0;
      	}
      

    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   |
            |   1  |   439  |   1   |
            |   2  |    43  |   2   |
            |   3  |     4  |   3   |
            |   4  |     0  |   4   |
            -------------------------
      
      So, what does the program finally print? 4
                                                            +--------+
                                                            |    /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 )
         {
            int i;
     
            for (i = 2; i < n; i++)
               if (n % i == 0) return 0;
    
            return 1;
         }
    
                                                            +--------+
                                                            |    /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 :

    
       float hat ( float x)
       {
          if (x < -1)
             return 0;
          else if (x < 0)
             return 1+x;
          else if (x < 1)
             return 1-x;
          else
             return 0;
       }
             
                                                            +--------+
                                                            |    /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.
    /* MCS 260 exam 1 question : counting coins
       Takes a sequence of characters on input, like pndqdnp
          p = penny   $0.01;
          n = nickel  $0.05;
          d = dime    $0.10; 
          q = quarter $0.25.
       Prints the value of the sum of the coins.   */
    
    #include<stdio.h>
    
    int main(void)
    {
       float sum = 0.0;
       char coin;
    
       while (scanf("%c", &coin) > 0)
          switch(coin)
          {
             case 'p': sum += 0.01; break;
             case 'n': sum += 0.05; break;
             case 'd': sum += 0.10; break;
             case 'q': sum += 0.25; break;
          }
    
       printf("value of coins : $%.2f\n", sum);
    
       return 0;
    }
                                                            +--------+
                                                            |    /30 |
                                                            +--------+