Programming in C, autumn 2007


Exercise 3

FRI Sep. 21 12:15-14:00 BK106

  1. Suppose, that MAX is a macro, defined like this:
    #define MAX(x,y)   x < y ? y : x
    
    Show the fully expanded form of the following expressions and calculate the values of these expressions:
    1 + MAX(2,3)
    1 + MAX(3,2)
    MAX(2,3) + 1
    MAX(3,2) + 1
    
    Write a macro ISLOWER(c) which gives 1, if c is a lowercase character, and 0 otherwise.

    Write a macro MED(a,b,c), which prints 1 if the value of b is between a and c; otherwise it prints 0. Assume a, b, c are integers.

  2. Find the error in each of the following code segments and explain how to correct it.
    1.   x= 1
        while ( x <= 10);
          x++;
        }
      
    2.   for (y = .1; y!=1.0; y += .1)
          printf( "%f\n", y);
      
    3.   switch (n) {
        case 1:
          printf( "The number is 1\n");
        case 2:
          printf( "The number is 2\n");
          break;    
        default:
          printf( "The number is not 1 or 2\n");
          break;
        }    
      
    4. The following code should print the values 1 to 10
        n = 1;
        while (n < 10)
          printf( "%d ", n++);
      
  3. Write a function that takes the time as three integer arguments (for hours, minutes, and seconds), and returns the number of seconds since the last time the clock "struck 12". Use this fuction to calculate the amount of time in seconds between two times, both of which are within one 12-hour cycle of the clock.
  4. Write a function diamond that prints a diamond shape. The function gets as a parameter a character c and an odd number n. The function prints a diamond of n rows using a character c. Write also a main program, which you can use to test the fuction. An example, where the character is 'x' and the odd number n is 9.
                                                      x
                                                     x x
                                                    x x x
                                                   x x x x 
                                                  x x x x x
                                                   x x x x 
                                                    x x x
                                                     x x
                                                      x
    
  5. The Towers of Hanoi.

    According to an ancient legend, God, when creating the world, placed three diamond needles standing in an Asian temple. Ever since then, priests are attempting to move a stack of golden disks from one needle to another. The initial stack had 64 disks threaded onto one needle (A) and arranged from bottom to top by decreasing size. The priests are attempting to move the stack of disks from the needle A to another needle B using a third needle C. The following rules restrict the moving: According to the legend, the World will end when the priests have moved all disks to needle B.

    Write a recursive program, which gives advice on how to move disks using following commands:
    Move a disk from the needle A to the needle B.
    Test your program using a small number of disks (4-10).