CS 171 & 171L Introduction to Computer Science I & Lab Fall 2017

## Lab 10: 2D Arrays

### Goals and Objectives

The main goal this lab is for you to get a more practice with Java's multidimensional array storage structures. You will work more with loops to gain a better appreciation for their power and capabilities.

You will complete methods in a simple class that perform some 2D array operations.

### Background

Two-dimensional arrays have a wide variety of applications. Images are commonly represented on a computer using a 2D structure. The cells in a spreadsheet are referenced by rows and columns. Tables of data can be represented by a 2D array. Generally, two nested for loops are used to process every element in a 2D array.

### Introductions

Introduce yourself to your lab partner(s). What are your favorite board games? If your favorite game was on a computer, could you use a 2D array to represent the game board?

Download the source file Lab2DArrays.java. It contains the stubs of several static methods. Save it directly to your folder with your other lab files and open it in DrJava. Complete the methods as shown below. After you complete each method, demonstrate it to your instructor. Change the driver/navigator roles after each method.

 ``` // Your names here! public class Lab2DArrays { // print a 1D array // WORKS AS IS public static void print(int [] a) { System.out.print("{ "); for (int i = 0; i < a.length; i++) { System.out.printf("%d ", a[i]); } System.out.println("}"); } // Print a 2D array // WORKS AS IS public static void print(int [][] a) { for (int i = 0; i < a.length; i++) { if (i == 0) System.out.print("{ "); else System.out.print(" "); System.out.print("{ "); for (int j = 0; j < a[i].length; j++) { System.out.printf("%2d ", a[i][j]); } System.out.print("} "); if (i == a.length-1) System.out.print("}"); System.out.println(); } } // returns a copy of the input array // WORKS AS IS public static int[][] clone(int [][] a){ int [][] b; b = new int[a.length][]; for (int i = 0; i < a.length; i++){ b[i] = new int [a[i].length]; } for(int n = 0; n < b.length; n++){ for(int m = 0; m < b[n].length; m++){ b[n][m] = a[n][m]; } } return b; } // return maximum element of array a // WORKS AS IS public static int max(int [][] a) { int max = a[0][0]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) if (a[i][j] > max) max = a[i][j]; } return max; } // Returns a ragged array with r rows containing // {{1}, {2,3}, {4,5,6}, ...} public static int[][] triangle(int r){ return new int[1][1]; } // return a ragged 2D array containing Pascal's triangle p(i,j) // if j=0 or j=i, then p(i,j) = 1 // else p(i,j) = p(i,j) = p(i-1, j-1) + p(i-1,j) // See class notes! public static int [][] pascal(int n) { int[][] p = new int[n][]; // allocate space for n rows return p; } // return true arrays a and b contain the same elements // if arrays have different sizes, return false public static boolean equals(int [][] a, int [][] b) { return false; } // return true if e is an element of array a public static boolean contains(int [][] a, int e) { return false; } // add arrays a and b element by element // Assume a and b have same size, but may be ragged public static int [][] add(int [][] a, int [][] b) { return new int[1][1]; } // transpose a (flip along NW-SE diagonal) // Assume a is rectangular // exchanges element at i,j with element at j,i // What are the dimensions of the output array? public static int [][] transpose(int [][] a) { return new int [1][1]; } // return subarray of a between rows i1 and i2 and columns j1 and j2 (inclusive) public static int [][] subArray(int [][] a, int i1, int i2, int j1, int j2) { return new int [1][1]; } // replace the portion of a at (n, m) with s // Assume a and b are rectangular public static int [][] replace(int[][] a, int [][] s, int n, int m) { return new int[1][1]; } // returns an array that is the sum of the rows of a // Use a single loop public static int [] rowSum(int [][] a) { return new int [1]; } // returns an array that is the sum of the columns of a // Use a single loop public static int [] colSum(int [][] a) { return new int [1]; } // returns an array that is the sum of the NE-SW diagonal of a // what is the dimension of the array you return? // Use a single loop public static int [] diagSum1(int [][] a) { return new int [1]; } // returns an array that is the sum of the NW-SE diagonal of a // what is the dimension of the array you return? // Use a single loop public static int [] diagSum2(int [][] a) { return new int [1]; } // returns true if a is a magic square // a magic square is a square array of consecutive intergers from 1 to n*n // each of the rows, columns, and major diagonals add to the same number // Use the previous functions public static boolean isMagicSquare(int [][] a) { return false; } // insert a row at index n // place 0 at each inserted element // Assume a is rectangular public static int [][] insertRow(int[][] a, int n) { return new int[1][1]; } // insert a column at index n // put 0 in each element // Assume a is rectangular public static int [][] insertColumn(int[][] a, int n) { return new int[1][1]; } // delete a row at index n // Assume a is rectangular public static int [][] deleteRow(int[][] a, int n) { return new int[1][1]; } // delete a column at index n // Assume a is rectangular public static int [][] deleteColumn(int[][] a, int n) { return new int[1][1]; } // rotate a clockwise // Assume a is rectangular public static int [][] rotate(int [][] a) { return new int [1][1]; } // fills a triangular array with the integers in a serpentine pattern // serpentine(4) // 1 2 6 7 // 3 5 8 // 4 9 // 10 public static int [][] serpentine(int n) { return new int[1][1]; } // main method for testing public static void main(String args[]) { int a[][] = {{1,2,3},{4,5,6},{7,8,9}}; print(a); System.out.println("\ntriangle:"); int [][] t = triangle(3); print(t); System.out.println("\nclone:"); int [][] ca = clone(a); print(ca); int [][] ct = clone(t); print(ct); System.out.println("\nequals:"); System.out.println(equals(a,a)); System.out.println(equals(a,ca)); System.out.println(equals(a,ct)); ca[2][2] = -1; System.out.println(equals(a,ca)); System.out.println("\nmax:"); System.out.println(max(a)); System.out.println("\ncontains:"); System.out.println(contains(a,-1)); System.out.println(contains(a,3)); System.out.println("\nPascal:"); int [][] p = pascal(10); print(p); System.out.println("\nAdd:"); int b[][] = {{2,4,6},{8,10,12},{14,16,18}}; int [][] ab = add(a,b); print(ab); System.out.println("\nRowSum:"); int [] rs = rowSum(p); print(rs); System.out.println("\nColsum:"); int [] cs = colSum(p); print(cs); System.out.println("\ndiagSum:"); int [] ds = diagSum1(p); print(ds); int [] ds1 = diagSum2(a); print(ds1); System.out.println("\nSubArray:"); int [][] ps = subArray(p,6,8,3,5); print(ps); System.out.println("\nTranspose:"); int [][] rect1 = {{1, 2, 3, 4}, {5, 6, 7, 8}}; int [][] rect2 = transpose(rect1); print(rect2); System.out.println("\nRotate:"); int [][] rect3 = rotate(rect1); print(rect3); System.out.println("\nMagic Square:"); System.out.println(isMagicSquare(a)); int m1[][] = {{2,7,6},{9,5,1},{4,3,8}}; System.out.println(isMagicSquare(m1)); int m2[][] = {{16,3,2,13},{5,10,11,8},{9,6,7,12},{4,15,14,1}}; System.out.println(isMagicSquare(m2)); int m3[][] = { {17, 24, 1, 8, 15}, {23, 5, 7, 14, 16}, {4, 6, 13, 20, 22}, {10, 12, 19, 21, 3}, {11, 18, 25, 2, 9}}; System.out.println(isMagicSquare(m3)); System.out.println("\nInsert Row:"); print(m3); int [][] m1ir1 = insertRow(m3,2); print(m1ir1); int [][] m1ir2 = insertRow(m1ir1,0); print(m1ir2); int [][] m1ir3 = insertRow(m1ir2,m1ir2.length); print(m1ir3); System.out.println("\nDelete Row:"); m1ir2 = deleteRow(m1ir3,0); print(m1ir2); System.out.println("\nInsert Column:"); print(m3); int [][] m1ir4 = insertColumn(m3,2); print(m1ir4); int [][] m1ir5 = insertColumn(m1ir4,0); print(m1ir5); int [][] m1ir6 = insertColumn(m1ir5,m1ir5[0].length); print(m1ir6); System.out.println("\nDelete Column:"); m1ir5 = deleteColumn(m1ir6,0); print(m1ir5); System.out.println(); print(m3); int [][] m3in = replace(m3, a, 2, 2); print(m3in); System.out.println("\nSerpentine:"); int [][] s = serpentine(8); print(s); } } ```

### Getting Started

In this lab, you will practice writing loops. You should start off with s1 driving and s2 navigating. Complete the methods in ArrayStuff.java. You should change driver/navigator roles after each method is completed.

There are several independent methods in this lab. These are listed in roughly easiest to hardest, but if you get stuck on a part, you should consider trying the next step.

Send me your .java file at the end of lab. You should try and complete any methods you did not finish in class.

Have Fun!