CS13002 Programming and Data Structures

Spring semester

Tutorial I

Exercises

  1. Exercise 1 : On variables and assignments
  2. Exercise 2 : On binary representation of numbers
  3. Exercise 3 : On loop invariance
  4. Exercise 4 : On functions
  5. Exercise 5 : On recursive functions
  6. Exercise 6 : On parameter passing
  7. Exercise 7 : More on recursive functions
  8. Exercise 8 : Number representation

Exercise 1

What are the values printed by the following three programs?

  1.    #include <stdio.h>
       const int N = 10 - 5;
       int a;
       main () {
          a = N - N;
          printf("a = %d\n", a);
       }
    

  2.    #include <stdio.h>
       #define N 10 - 5
       int a;
       main () {
          a = N - N;
          printf("a = %d\n", a);
       }
    

  3.    #include <stdio.h>
       #define N 10 - 5;
       int a;
       main () {
          a = N - N;
          printf("a = %d\n", a);
       }
    

Solution Top

Exercise 2

Write a function for each of the following problems:

  1. Given a positive integer n, print the binary representation of n.

  2. Given an integer n, print the 32-bit 2's complement representation of n.

  3. Given a positive real number, print the binary representation of n.

Solution Top

Exercise 3

Find an invariant for the following loop:

   int n, t;
   float x, y;

   n = 0;
   x = 1 + rand() % 9;
   y = 1 + rand() % 9;
   while (n < 10) {
      t = 1 + rand() % 9;
      x *= t;
      y /= t;
      ++n;
   }

Solution Top

Exercise 4

Determine what each of the following functions do.

  1.    double foo4 ( float A[] , int n )
       {
          float s, t;
    
          s = t = 0;
          for (i=0; i<n; ++i) {
             s += A[i];
             t += A[i] * A[i];
          }
          return (t/n)-(s/n)*(s/n);
       }
    

  2.    int foo5 ( unsigned int n )
       {
          if (n == 0) return 0;
          return 3*n*(n-1) + foo5(n-1) + 1;
       }
    

Solution Top

Exercise 5

Write a recursive function that, given a string of given length, determines whether the string is a palindrome.

Solution Top

Exercise 6

Determine what the following program prints.

   #include <stdio.h>

   void foo ( int n , int A[] )
   {
      n = 0; A[n] = 3;
      n = 1; A[n] = 5;
      n = 2; A[n] = 7;
      n = 3; A[n] = 11;
      n = 4; A[n] = 13;
   }

   int main ()
   {
      int n = 1, A[5] = {2,3,5,7,11};

      foo(n,A);
      printf("A[n] = %d\n", A[n]);
   }

Solution Top

Exercise 7

Two frogs are sitting at the bottom of a flight of 10 steps and debating in how many ways then can jump up the stairs. They can jump one, two or three steps at once. For example, they can cover the 10 steps by jumping (3,3,3,1) or (2,3,2,1,2) or other suitable combinations of steps. Their mathematics is not very strong and they approach you for help in order to find out the total number of possibilities they have to reach the top. Please provide them with a general solution (not only for 10 but for general n steps) in the form of a C function. Note that the order of the steps is important here, i.e., (3,3,3,1) is treated distinct from (1,3,3,3) for example.

Solution Top

Exercise 8

Find the 32-bit floating point representation of +35.375 under the IEEE floating point standard. Also of -35.375.

Solution Top


Course home