CPS 006, Spring 2000, Quiz Questions, April 10 - April 14

Two or three of these questions will be used for an in-class quiz at the end of the week. It's possible that the questions may be modified slightly. The class tmatrix is described in Chapter 10, it's a two-dimensional version of a vector. For example:
 tmatrix<int> a(5,3); // a has 5 rows and 3 columns
 a[4][2] = 21;        // store 21 in last row/last column

  1. In Pause/Reflect exercise 10.20 the functions RowSum and ColSum are described. RowSum is shown below, you should write ColSum
  2. int RowSum(const tmatrix<int>& m, int r)
    // pre: 0 <= r < m.numrows()
    // post: returns sum of numbers in row r
    {
        int k;
        int sum = 0;
        for(k=0; k < m.numcols(); k++)
        {
            sum += m[r][k];
        }
        return sum;
    }
    
    
    Write ColSum
    int ColSum(const tmatrix<int>& m, int c)
    // pre: 0 <= c < m.numcols()
    // post: returns sum of numbers in column c
    
    
    
    
  3. Write a function DiagonalSum that returns the sum of the entries on the main (principal) diagonal of a square matrix (a square matrix has the same number of rows as columns).

  4.  

     

    The main diagonal of a matrix m consists of the elements

     m[0][0], m[1][1], m[2][2], ...
    and, in general, of m[k][k] as k ranges from 0 to m.numrows()-1.
     int DiagonalSum(const tmatrix<int>& m)
     // pre: m is a square matrix
     // post: returns sum of elements on main diagonal
    
    
    
  5. See Pause/Reflect 10.21. A magic square consists of numbers arranged ina matrix so that the rows, columns and both diagonals sum to the same number. For example, the matrix below is magic, and the magic sum is 15.
  6.   6 1 8
      7 5 3
      2 9 4
    Write IsMagic that returns true if its matrix parameter m is magic. You can call RowSum, ColSum, and DiagonalSum.
      bool IsMagic(const tmatrix<int>& m)
      // pre: m is a square matrix
      // post: returns true if m is a magic square, false otherwise
    
    
  7. Write the recursive function Search below that returns true if key is contained in list, and false otherwise. You cannot write any loop, just make one recursive call and use one if statement in addition to what's written. Note that the second function Search below is NOT recursive, it's calling the first version (the one you write) that's a different function because the parameter list is different.
  8.  bool Search(const tvector<string>& list, const string& key, int index)
     // pre: 0 <= index < list.size(), list contains list.size() elements
     // post: returns true if key contained in list[index]...list[list.size()-1]
     {
        if (index >= list.size()) return false;
    
    
    
     }
     bool Search(const tvector<string>& list, const string& key)
     // pre: list contains list.size() elements
     // post: returns true if key in list, false otherwise
     {
        return Search(list, key, 0);  // uses helper function
     }
    
    
  9. Write a function Mode that finds the number that occurs most frequently in a tvector a. Assume that all the elements in a are between 0 and 100 (inclusive). There are many ways of writing this function. You should try to write a function that executes quickly (don't worry about storage, worry about time).
  10.   int Mode(const tvector<int>& a)
      // pre: a contains a.size() elements, all between 0 and 100 (inclusive)
      // post: returns number in a that occurs more frequently than any other
      //       (assume no ties)