Answer to Quiz 1 Fri 12 Jan 2001


1. Consider the function Fbp below.

   Write next to the code for Fbp the equivalent function Fba
   that uses arrays instead of pointers.  Equivalent means that
   calling Fbp has the same effect as calling Fba.
     
void Fbp (int *f, int n)                void Fba (int f[], int n)

{                                       {

   int *p;                                 int i;

   *f = 1;                                 f[0] = 1;

   *(f+1) = 1;                             f[1] = 1;

   for (p = f+2; p < f+n; ++p)             for (i = 2; i < n; ++i)

      *p = *(p-1) + *(p-2);                   f[i] = f[i-1] + f[i-2];

}                                       }

2 Write a function max that takes as input an integer
  m x n-matrix a of arbitrary dimensions
  (so m and n are arguments of max as well).
  The output of max is the maximal element of a.
  You must use arrays.

  int max (int m, int n, int a[m][n])
  {
     int i,j,result = a[0][0];
     for (i = 0; i < m; ++i)
        for (j = 0; j < n; ++j)
           if (a[i][j] > result)
              result = a[i][j];
     return result;
  }