MCX1/2                                                                                                               Mr.Stanley Teitel, Principal
Mr. Gary Jaye                                                                                                       Mr. Danny Jaye, Math Chair

                        1996 Question 4
from APCS A Exam: Part II (adapted for Java)

The class Mat, which implements a matrix of integers, is defined as follows: 
class Mat {    
    private int[][] entries;                     // storage for matrix of integers    
    public int get(int r, int c) {...}           // returns entries[r][c] 
    public int numRows() {...}                   // returns # of rows 
    public int numCols() {...}                   // returns # of cols 
    public void set(int r, int c, int val){...}  // sets entries[r][c] to val 
    public void resize(int r, int c) {...}       // resizes array entries to r rows, c columns
                                                 //   data in positions that remain is preserved
    < other methods not shown >
}//=== Mat
Part A
Write method SumCross, whose header is given below. SumCross returns the sum of the values 
in the row with index R and the column with index C from the Mat object m. The value in 
position (R, C) is included in the sum only once. For example, consider the Mat object  
m, diagrammed below, where m.numRows() == 3 and m.numCols() == 4 
               +------+------++----++------+   
               |      |      ||    ||      |   
               |  77  |  22  ||  1 ||  33  |   
               |      |      ||    ||      |   
               +======+======++====++======+
               |   5  |   3  || 10 ||  4   |   
               |      |      ||    ||      |
               +======+======++====++======+   
               |      |      ||    ||      |   
               |  66  |  44  ||  2 ||  55  |   
               |      |      ||    ||      |     
               +------+------++----++------+   
The call SumCross(M,1,2) returns the value 25, which is the sum of the values in the row with 
index 1 and the column with index 2, including the value 10 only once. 
Complete method SumCross below the following header. Assume that SumCross is called only with 
parameters that satisfy its precondition. 
      public static int sumCross(final Mat m, int r, int c) {
      // precondition: 0 <= r < m.numRows(); 0 <= c < m.numCols()


Part B
Write the method RemoveCross, whose header is given below. RemoveCross removes the row with index 
R and the column with index C from Mat object m. For example, consider Mat object m diagrammed 
below, where m.numRows() == 5 and m.numCols() == 6. After the call RemoveCross(m,2,3), the
original row with index 2 and column with index 3 have been removed and m.numRows() == 4 and 
m.numCols() == 5. 
                 
	+----+----+----++---++----+----+       +----+----+----+----+----+
	| 11 | 22 | 33 || 5 || 44 | 55 |       | 11 | 22 | 33 | 44 | 55 |
	+----+----+----++----+----+----+       +----+----+----+----+----+
	| 22 | 33 | 44 || 6 || 55 | 66 |       | 22 | 33 | 44 | 55 | 66 |
	+====+====+====++===++====+====+       +----+----+----+----+----+
	|  4 |  5 |  6 || 7 ||  8 |  9 |       | 33 | 44 | 55 | 66 | 77 |
	+====+====+====++===++====+====+       +----+----+----+----+----+
	| 33 | 44 | 55 || 8 || 66 | 77 |       | 44 | 55 | 66 | 77 | 88 |
	+----+----+----++---++----+----+       +----+----+----+----+----+
	| 44 | 55 | 66 || 9 || 77 | 88 |         
	+----+----+----++---++----+----+         
Complete the method RemoveCross below the following header. Assume that RemoveCross is called only 
with parameters that satisfy its precondition. 
      public static void removeCross(Mat m, int r, int c)  {
      // precondition: 0 <= r < m.numRows(); 0 <= c < m.numCols()
      // postcondition: row with index r and column with index c are removed from m

//=======================================================================================
// A96_4.java        
/* CORRECT OUTPUT:
 * 
Mat M: 
	77	22	1	33
	5	3	10	4
	66	44	2	55

sumCross(M, 1, 2) = 25
sumCross(M, 0, 3) = 192

Mat M: before removeCross(2, 3)
	11	22	33	5	44	55
	22	33	44	6	55	66
	4	5	6	7	8	9
	33	44	55	8	66	77
	44	55	66	9	77	88

Mat M: after removeCross(2, 3)
	11	22	33	44	55
	22	33	44	55	66
	33	44	55	66	77
	44	55	66	77	88

=====================================================================*/
public class A96_4  {  

      public static int sumCross(final Mat m, int r, int c) {
      // precondition: 0 <= r < m.numRows(); 0 <= c < m.numCols()
        return -77;
      }//=== sumCross
      
      
      public static void removeCross(Mat m, int r, int c)  {
      // precondition: 0 <= r < m.numRows(); 0 <= c < m.numCols()
      // postcondition: row with index r and column with index c are removed from m
 
    }//=== removeCross
      
//================================================================================
    public static void main(String[] args) {
        sumCrossTest();
        removeCrossTest();
    }//=== main
    
    static void sumCrossTest() {
          int[][] A = {{77, 22,  1, 33}, { 5,  3, 10,  4}, {66, 44,  2, 55} };
          Mat M = new Mat(A.length, A[0].length);
          for (int r = 0; r < M.numRows(); r++) {
              for (int c = 0; c < M.numCols(); c++) M.set(r, c, A[r][c]);              
          }
          print(M);
          System.out.println("\nsumCross(M, 1, 2) = " + sumCross(M, 1, 2));
          System.out.println("sumCross(M, 0, 3) = " + sumCross(M, 0, 3) + "\n");        
    } //=== sumCrossTest
    
    static void removeCrossTest() {
        int [][] A = {
            {11, 22, 33,  5, 44, 55},
            {22, 33, 44,  6, 55, 66},
            { 4,  5,  6,  7,  8,  9},
            {33, 44, 55,  8, 66, 77},
            {44, 55, 66,  9, 77, 88} };
        Mat M = new Mat(A.length, A[0].length);
        for (int r = 0; r < M.numRows(); r++) {
            for (int c = 0; c < M.numCols(); c++) M.set(r, c, A[r][c]);              
        }           
        print(M, "before removeCross(2, 3)");
        removeCross(M, 2, 3); System.out.println();
        print(M, "after removeCross(2, 3)");
   } //=== removeCrossTest
 
    static void print(final Mat M, String msg) {
        System.out.println("Mat M:" + " " + msg);
        for (int r = 0; r < M.numRows(); r++) {
            for (int c = 0; c < M.numCols(); c++)
                System.out.print("\t" + M.get(r,c)); 
            System.out.println();
        }
    } //=== print
   
    static void print(final Mat M)  {
        print(M, "");
    }
}//=== A96_A
    
class Mat {    
    private int[][] entries;   // storage for matrix of integers        
    public Mat(int r, int c) { entries = new int[r][c]; }  //== constructor
    public int get(int r, int c) { return entries[r][c]; } //== accesses [r][c]
    public int numRows() { return entries.length; }         //== returns # of rows
    public int numCols() { return entries[0].length; }      //== returns # of cols
    public void set(int r, int c, int val)                  //== sets [r][c] to val 
         { entries[r][c] = val; }           
    public void resize(int r, int c) {       //== resizes entries to r rows, c columns
        int[][] temp = new int[r][c];
        int sourceRow = r < numRows() ? r : numRows();
        int sourceCol = c < numCols() ? c : numCols();
        for (int row = 0; row < sourceRow; row++)
            for (int col = 0; col < sourceCol; col++)
                temp[row][col] = entries[row][col];
        entries = temp;                
    }//=== resize
}//=== Mat

//============= end of file A96_A.java    



                                        Free Response Questions       MCX1 home       MCX2 home