CS 176, Introduction to Computer Science II

Summer 2001 - Quiz 5


The procedure order accepts three integer parameters. After a call to order, the values in the parameters are such that

  1. the value in first (that is, left most) parameter is less than or equal to the value in the second (that is, middle) parameter, and

  2. the value in second parameter is less than or equal to the value in the third (that is, rightmost) parameter
Further, the original values of the parameters are not lost during the call to order; that is, after the call each parameter has a value held by a parameter before the call.

For example, if the values of the parameters to order before the call are 2, 3, and 1 in left to right order, than after the call to order, the values in the parameters will be 1, 2, and 3.

  1. Write a prototype for order.


    With pointers it's

    void order(int * ip, int * jp, int * kp);
    
    with references it's
    void order(int & i, int & j, int k);
    
    References are not pointers, so it goes against convention to use a naming sheme that suggests they are pointers. You weren't expected to know references for this quiz, but some people did use them and you should know about them now.


  2. Write the code for the function order.


    A pointer-based implementation

    static void swap_if_greater(int * ip, int * jp) {
    
      // Permute the values *ip and *jp so that *ip <= *jp.
    
      if (*ip > *jp) {
        int t = *ip;
        *ip = *jp;
        *jp = t;
        }
      }
    
    void order(int * ip, int * jp, int * kp) {
    
      // Permute the values *ip, *jp, and *kp so that *ip <= *jp <= *kp.
    
      swap_if_greater(ip, jp);
    
      // *ip <= *jp.
    
      swap_if_greater(ip, kp);
    
      // *ip <= *jp and *ip <= *kp.
    
      swap_if_greater(jp, kp);
    
      // *ip <= *jp and *ip < *kp and *jp <= *kp.
      }
    
    followed by a reference-based implementation:
    static void swap_if_greater(int & i, int & j) {
    
      // Permute the values i and j so that i <= j.
    
      if (i > j) {
        int t = i;
        i = j;
        j = t;
        }
      }
    
    void order(int & i, int & j, int & k) {
    
      // Permute the values i, j, and k so that i <= j <= k.
    
      swap_if_greater(i, j);
    
      // i <= j.
    
      swap_if_greater(i, k);
    
      // i <= j and i <= k.
    
      swap_if_greater(j, k);
    
      // i <= j and i < k and j <= k.
      }
    


  3. Write a small sample of code (one test case containing one order call) that you might use to check the behavior of your order implementation.


    A pointer-based test:

    int i = 2;
    int j = 3;
    int k = 1;
    order(&i, &j, &k);
    if ((i != 1) || (j != 2) || (k != 3))
      cerr << "values out of order.\n";
    
    A reference-based test:
    int i = 2;
    int j = 3;
    int k = 1;
    order(i, j, k);
    if ((i != 1) || (j != 2) || (k != 3))
      cerr << "values out of order.\n";
    


This is not a design test; your answers should be given in accurate C++ code.


This page last modified on 8 August 2001.