CS13002 Programming and Data Structures

Spring semester

Tutorial I : Solutions

Solution of Exercise 1

  1. a = 0
  2. a = -10 (literal substitution gives a = 10 - 5 - 10 - 5;)
  3. a = 5 (literal substitution gives a = 10 - 5; - 10 - 5;;)

Back to Exercise





























Solution of Exercise 2

  1.    #include <stdio.h>
    
       void binrep ( unsigned int n )
       {
          if (n <= 1) { printf("%d",n); return; }
          binrep(n/2);
          printf("%d",n%2);
       }
    
       int main ()
       {
          unsigned int n;
          printf("n = "); scanf("%u",&n);
          printf("Binary representation: ");
          binrep(n);
          printf("\n");
       }
    

  2.    #include <stdio.h>
    
       void twocrep ( int n , int A[] )
       {
          int i;
    
          if (n == -2147483648) {
             printf("10000000000000000000000000000000\n");
             return;
          }
          if (n < 0) { A[31] = 1; n = -n; } else { A[31] = 0; }
          for (i=0; i<=30; ++i) {
             if (A[31]==0) A[i] = n % 2; else A[i] = 1 - n % 2;
             n = n / 2;
          }
          if (A[31] == 1) {
             i = 0;
             while (A[i] == 1) { A[i] = 0; ++i; }
             A[i] = 1;
          }
          for (i=31; i>=0; --i) printf("%d",A[i]);
          printf("\n");
       }
    
       int main ()
       {
          int n, A[32];
          printf("n = "); scanf("%d",&n);
          printf("2's complement representation: ");
          twocrep(n,A);
       }
    

  3.    #include <stdio.h>
    
       void leftrep ( unsigned int n )
       {
          if (n <= 1) { printf("%d",n); return; }
          leftrep(n/2);
          printf("%d",n%2);
       }
    
       void rightrep ( float x )
       {
          int i, precision = 10;
    
          for (i=0; i<precision; ++i) {
             x = x * 2;
             printf("%d",(int)x);
             x = x - (int)x;
          }
       }
    
       int main ()
       {
          float x;
          printf("x = "); scanf("%f",&x);
          printf("Binary representation: ");
          leftrep((int)x);
          printf(".");
          rightrep(x - (int)x);
          printf("\n");
       }
    

Back to Exercise





























Solution of Exercise 3

The product x * y remains constant (up to floating point approximation).

Back to Exercise





























Solution of Exercise 4

  1. The function computes and returns the standard deviation of the elements A[0],A[1],...,A[n-1].
  2. The function computes and returns n3 upon input n.

Back to Exercise





























Solution of Exercise 5

   int isPalindrome ( char A[] , int start, int n )
   {
      if (n <= 1) return 1;
      if (A[start] != A[start+n-1]) return 0;
      return isPalindrome(A,start+1,n-2);
   }

Back to Exercise





























Solution of Exercise 6

   A[n] = 5

Back to Exercise





























Solution of Exercise 7

   int frog ( int n )
   {
      if (n <= 0) return 0;
      if (n == 1) return 1;   /* Only one possibility: (1) */
      if (n == 2) return 2;   /* Two possibilities: (1,1) and (2) */
      if (n == 3) return 4;   /* Four possibilities: (1,1,1), (1,2), (2,1) and (3) */
      return frog(n-1) + frog(n-2) + frog(n-3);
   }

Back to Exercise





























Solution of Exercise 8

We have

     35
   = 32 + 2 + 1
   = 25 + 21 + 20
   = (100011)2.

     0.375
   = 0.25 + 0.125
   = (1/4) + (1/8)
   = 2-2 + 2-3
   = (0.011)2.

Therefore,

     35.375
   = (100011.011)2
   = (1.00011011)2 x 25
   = (1.00011011)2 x 2[132 - 127]
   = (1.00011011)2 x 2[(128 + 4) - 127]
   = (1.00011011)2 x 2[(10000100)2 - 127]
   = (1.00011011000000000000000)2 x 2[(10000100)2 - 127]

Consequently, the 32-bit floating point representation of +35.375 is:

   0 10000100 00011011 00000000 0000000

and that for -35.375 is:

   1 10000100 00011011 00000000 0000000

Back to Exercise






























Course home