COMP 1917 Computing 1
Session 2, 2016

Tutorial Solutions - Week 6


  1. Write a C function
    void swap (int* first, int* second);
    
    which takes in pointers to two integers first and second which are declared in the calling function, and then swaps them around so that when swap returns first now has the value that second initially held, and second now has the value that first initially held.

    void swap (int* first, int* second){
      int temp;
      temp = *first;
      *first = *second;
      *second = temp;
    }
    
  2. Explain what is the problem with the following function:
    void swap (int first, int second);
    
    When function is called the values are copied into first and second. Thus the values in main remain unchanged as any moifications will be performed on copies.
  3. Given these declarations
    int     n;
    int    *p, *q;
    double  x;
    double *r;
    
    What will happen when each of the following statements is executed (in order)?
    a.  p = &n; // p will point to n
    b. *p =  5; // 5 will be stored into n
    c. *q = 17; // the program will attempt to store 17 into the address given by q,
                // which could cause an error because q has not been initialized.
    d.  q =  p; // q will point to n
    e. *q =  8; // 8 will be stored into n
    f.  r = &x; // r will point to x
    g. *r = 3.0;// 3.0 will be stored into x
    h. *p = *r; // on most systems, this will not cause a problem, because there will
                // automatically be a type conversion from double to int, setting x to 3.
    i.  p = r;  // this is dangerous because p, which is supposed to point to int,
                // is being set to the address of x, which is a double.
    j. *p = n;  // the first four bytes of x will be set equal to the integer
                // representation for 3, causing it to have a "garbage" value.
    k.  n = x;  // the program will attempt to interpret the "garbage" value
                // stored in x, causing an error.
    
  4. double max_value(int n, double a[])
    {
      double max = a[0];
      int i;
    
      for( i=1; i < n; i++ ) {
         if( a[i] > max ) {
            max = a[i];
         }
      }
      return( max );
    }
    
  5. Write a function
    int non_decreasing( int a[], int N )
    
    which takes an integer N together with an array a[] of N integers and checks to see whether the items in the array are sorted in non-decreasing order. (i.e. a[i] ≥ a[i-1], for 0<i<N). Your function should returns 1 if the items are in non-decreasing order, 0 otherwise.
    #define TRUE  1
    #define FALSE 0
    
    int non_decreasing( int a[], int N )
    {
      int no_decrease = TRUE;
      int i;
      for( i=0; i < N-1 && no_decrease ; i++ ) {
        if( a[i] > a[i+1] ) {
          no_decrease = FALSE;
        }
      }
      return( no_decrease );
    }
    
  6. Write a function
    int findIndex(int x, int a[], int N)
    
    which takes two integers x and N together with an array a[] of N integers and searches for the specified value within the array. Your function should return the smallest index k such that a[k] is equal to x (or -1 if x does not occur in the array).
    int findIndex( int x, int a[], int N )
    {
      int k = -1;
      int i;
      for( i=0; i < N && k == -1; i++ ) {
        if( a[i] == x ) {
          k = i;
        }
      }
      return( k );
    }
    
  7. int find_in_grid( int x, int grid[SIZE][SIZE], int size){
       int i = 0;
       int j = 0;
       int found = FALSE;
    
       while(i < size){
          j = 0;
          while(j < size){
             if(grid[i][j] == x){
                found = TRUE;
             }
             j++;
          }
          i++;
       }
    
       return found;
    }