Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [20pts] We would like you to self-test how much you retained from the lecture. Submit prac.cpp as the correct solution to the practice problem of
    Find the hardest problem
    Try not to look at the solution. If you had to, briefly describe what you missed but understand now.
    
    
    
    
  2. [20pts] Assume the following delcarations and fill in the table with either the type of the given expression, or "error" if appropriate.
    
    double x;
    string s;
    double* A;
    int** M;
    string** W;
    
    expressiontype
    x
    s
    A
    M
    W
    x[0]
    s[0]
    A[0]
    M[0]
    W[0]
    expressiontype
    x[0][0]
    s[0][0]
    A[0][0]
    M[0][0]
    W[0][0]
    x[0][0][0]
    s[0][0][0]
    A[0][0][0]
    M[0][0][0]
    W[0][0][0]
  3. [5pts] Consider the following function prototype.
    
    void findMaxSum(double** A, int rows, int cols, int* pCol, double* pSum);
    
    The above function finds the column and the sum of the entries in that column such that its sum is the largest in all columns.
    The input parameters are as follows:
    • A: 2D array
    • rows: #rows in the array
    • cols: #columns in the array
    • pCol: points to an object that the function should put the column
    • pSum: points to an object that the function should put the sum

    Your Task: Fill in the blank on the right box with the correct function call to findMaxSum.

    • Assume variables Arr, n, m have been set correctly.
    
    int main()
    {
    
      double** Arr;  // 2D array 
      int n, m;      // # rows and # cols
      int col; 
      double sum;
    
      ... // some code 
      
      // Assume: Arr, n, m have been set correctly.
      // FILL BELOW: call findMaxSum() 
    
    
      ---------------------------------------
      cout << "Column " << col 
           << " has the maximum sum with " << sum << endl;
    }
    
  4. [55pts] Write a program hw.cpp that works as follows:

    ~/$ ./hw
    What size? 2 x 3 
    1 8 0
    4 7 7 
    rows: 0 1 
    cols: 1 

    In particular:

    1. The program asks the user to specify a size n x m of a matrix of integers.
    2. It reads in n x m numbers.
    3. It prints out the indices of rows and columns such that the sum of numbers in that row or column is a multiple of 3.
    For the above run, Therefore, both of rows 0 and 1 are listed in the output. Therefore, only column 1 is listed in the output.

    Here are some more example runs. Your program must read input and produce output with the exact same formatting! The red text represents the user input.

    ~/$ ./hw
    What size? 1 x 1
    6
    rows: 0 
    cols: 0 
    
    ~/$ ./hw
    What size? 1 x 1
    5
    rows:
    cols:
    
    ~/$ ./hw
    What size? 3 x 3 
    6 9 5
    4 6 8
    9 4 14
    rows: 1 2 
    cols: 2 
    
    ~/$ ./hw
    What size? 5 x 3
    3 4 1 
    6 2 3
    2 4 5
    1 3 2
    3 2 4
    rows: 3 4 
    cols: 0 1 2
    
    ~/$ ./hw
    What size? 3 x 5
    3 6 2 1 3 
    4 2 4 3 2 
    1 3 5 2 4 
    rows: 0 1 2 
    cols: 3 4 
    

Turn in