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

                                 1995 Question 2 from APCS A Exam: Part II (adapted for Java)

In parts (a) and (b) of this problem you may use method Swap, which interchanges the values of its two parameters.

     static void swap(int[] A, int index1, int index2)
     //postcondition: values at A[index1] and A[index2] are swapped

    static void swap(int[][] M, int col, int row1, int row2)
     //postcondition: values at M[row1][col] and M[row2][col] are swapped

part A
Write method reverseArray, whose header is given below. Method reverseArray rearranges the elements in A so
that they are in reverse order. For example, the result of reverseArray(a) is illustrated below.

    Before call to reverseArray(A)                           After call to reverseArray(A)

  +----+----+----+----+----+          +----+----+----+----+----+
  | 61 | 34 | 18 | 99 | 73 |          | 73 | 99 | 18 | 34 | 61 |
  +----+----+----+----+----+          +----+----+----+----+----+


Complete method reverseArray below the following header.

    public static void reverseArray(int[] A) {
    //postcondition: elements of A are reversed    

part B
Write method reverseVertical, whose header is given below. Method reverseVertical rearranges the elements of
one column of 2-dimensional array m so that they are in reverse order. For example, the result of
reverseVertical
(m,2) is illustrated below.

    Before call to reverseVertical(m,2)                      After call to reverseVertical(m,2)

    +----+----+----+----+                  +----+----+----+----+
    | 61 | 34 | 18 | 99 |                  | 61 | 34 | 35 | 99 |
    +----+----+----+----+                  +----+----+----+----+
    | 11 | 22 | 44 | 33 |                  | 11 | 22 | 77 | 33 |
    +----+----+----+----+                  +----+----+----+----+
    | 55 | 66 | 77 | 88 |                  | 55 | 66 | 44 | 88 |
    +----+----+----+----+                  +----+----+----+----+
    | 25 | 45 | 35 | 55 |                  | 25 | 45 | 18 | 55 |
    +----+----+----+----+                  +----+----+----+----+

Complete method reverseVertical below the following header.

    public static void reverseVertical(int[][] M, int col) {
    // postcondition: the elements in column index col of M are reversed 

part C
Write method reverseMatrix, whose header is given below. Method reverseMatrix rearranges the elements in m, a
N x N matrix (2-dimensional array), so that they are in reverse order. A matrix is reversed when both the rows are
reversed, i.e.,  r0, r1, r2,...,rn-1 becomes  rn-1,..., r2, r1,...,r0 and the elements within each row are reversed, as
was done in part (a). For example, the result of reverseMatrix(m), where N = 4 is illustrated below.

   Before call to reverseMatrix(m)                              After call to reverseMatrix(m)

    +----+----+----+----+                  +----+----+----+----+
    | 61 | 34 | 18 | 99 |                  | 55 | 35 | 45 | 25 |
    +----+----+----+----+                  +----+----+----+----+
    | 11 | 22 | 44 | 33 |                  | 88 | 77 | 66 | 55 |
    +----+----+----+----+                  +----+----+----+----+
    | 55 | 66 | 77 | 88 |                  | 33 | 44 | 22 | 11 |
    +----+----+----+----+                  +----+----+----+----+
    | 25 | 45 | 35 | 55 |                  | 99 | 18 | 34 | 61 |
    +----+----+----+----+                  +----+----+----+----+

In writing reverseMatrix, all array manipulations must be done using methods reverseArray and reverseVertical of
parts (a) and (b). This means that no array variable can appear on the left-hand side of the assignment operator (=).
You will receive no credit for part (c) if your code includes such assignment statements or calls to Swap. Assume that
reverseArray
and reverseVertical work as specified regardless of what you wrote for parts (a) and (b).

Complete method reverseMatrix below the following header.

    public static void reverseMatrix(int[][] M) {

//==============================================================================
// A95_2.java

public class A95_2 {
    public static void reverseArray(int[] A) {


    }//=== reverseArray

    public static void reverseVertical(int[][] M, int col) {


    }//=== reverseVertical

    public static void reverseMatrix(int[][] M) {
 

    }//=== reverseMatrix
//---------------------------------------------------------------------------
    public static void main(String[] args) {
        cls();
        System.out.println("\n\t1995 APCS A Exam: Question 2\n");
        driver();                 
    } //=== main

    static final String TAB = "\t";

    static void driver() {
        int A[] = {61, 34, 18, 99, 73};
        print(A, "Before reverseArray: ");
        reverseArray(A);
        print(A, "After reverseArray:  ");
        System.out.println();
        //--------------------------------
        int[][] M = {  {61, 34, 18, 99},
            {11, 22, 44, 33},
            {55, 66, 77, 88},
            {25, 45, 35, 55}  };
        print(M, "\nBefore reverseVertical:");
        reverseVertical(M, 2);
        print(M, "\nAfter reverseVertical:");
        //-----------------------------------
        swap(M, 2, 0, 3); swap(M, 2, 1, 2);
        print(M, "\nBefore reverseMatrix:");
        reverseMatrix(M);
        print(M, "\nAfter reverseMatrix:");  
    }//*** driver


    static void swap(int[] A, int index1, int index2) {
        int temp = A[index1]; A[index1] = A[index2]; A[index2] = temp;
    }    

    static void swap(int[][] M, int col, int row1, int row2) {
        int temp = M[row1][col];  M[row1][col] =  M[row2][col]; M[row2][col] = temp;
    }

    static void print(int[] v, String msg) {
        System.out.println(msg);
        for (int k = 0; k < v.length; k++)
            System.out.print( TAB + v[k]);
        if (msg.length() > 0 ) System.out.println();
    }//*** print

    static void print(int[][] m, String msg) {
        System.out.print(msg);
        for (int r = 0; r < m[0].length; r++) {
            print(m[r], "");    
        }
        System.out.println();
   }//*** print
      
    static void cls() {
        for (int k = 0; k < 55; k++) System.out.println(); 
    }   
} //================================================== end of A95_2.java file

 

Free Response Questions       MCX1 home       MCX2 home