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

## Lab 10: 2D Arrays

### Overview

I've recorded a video you can watch that is a overview of the lab.

### 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. Board games often use a 2D grid. Generally, two nested for loops are used to process every element in a 2D array.

Download the source file Lab10.pde. It contains the stubs of several methods. Save it directly to your folder with your other lab files and open it in Processing. Complete the methods as shown below.

### Code

 ``` // Your name(s) here void setup() { testing(); } void draw() { } void testing() { 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 ds1 = diagSum1(a); println(ds1); int ds2 = diagSum2(a); println(ds2); 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); } // Print a 1D array // WORKS AS IS void print(int [] a) { if (a == null) return; System.out.print("{"); for (int i = 0; i < a.length-1; i++) { System.out.printf("%d, ", a[i]); } System.out.printf("%d}\n", a[a.length-1]); } // Print a 2D array // WORKS AS IS void print(int [][] a) { if (a == null) return; for (int i = 0; i < a.length; i++) { if (i == 0) System.out.print("{"); else System.out.print(" "); System.out.print("{"); if (a[i] == null) continue; for (int j = 0; j < a[i].length-1; j++) { System.out.printf("%2d, ", a[i][j]); } System.out.printf("%2d}", a[i][a[i].length-1]); if (i == a.length-1) System.out.print("}\n"); else System.out.println(); } } // returns a copy of the input array // WORKS AS IS int[][] clone(int [][] a) { if (a==null) return null; // a is undefined 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 int max(int [][] a) { int max = a[0][0]; return max; } // Returns a ragged array with r rows containing // {{1}, {2,3}, {4,5,6}, ...} int[][] triangle(int r) { int t[][] = null; return t; } // 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) { // decare and allocate a triangular array named p int p[][] = null; return p; } // return true arrays a and b contain the same elements // if arrays have different sizes, return false // don't use a==b, that checks if a and b refer to the same array // which is different than containing the same elements // arrays may be ragged boolean equals(int [][] a, int [][] b) { return false; } // return true if e is an element of array a // array a may be ragged 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 // return a new array that is the sum of the input arrays int [][] add(int [][] a, int [][] b) { int c[][] = null; return c; } // transpose a (flip along NW-SE diagonal) // 1 2 3 ===> 1 4 // 4 5 6 2 5 // 3 6 // Assume a is rectangular // exchanges element at i,j with element at j,i // What are the dimensions of the output array? int [][] transpose(int [][] a) { int b[][] = null; return b; } // return subarray of a between rows i1 and i2 and columns j1 and j2 (inclusive) // like copying a rectangular region from an image // loop over every element in the output array - where did that come from? int [][] subArray(int [][] a, int i1, int i2, int j1, int j2) { int b[][] = null; return b; } // replace the portion of array a at upper left corner (n, m) with values from s // Assume a and s are rectangular and s fits into a // like pasting into an image int [][] replace(int[][] a, int [][] s, int n, int m) { int b[][] = null; return b; } // returns an array that is the sum of the rows of a // Use a single loop // the output array will have an element for each input row int [] rowSum(int [][] a) { return new int [1]; } // returns an array that is the sum of the columns of a // Use a single loop // assume the array is rectangular (all row has the same length as a[0].length) // the output array will have an element for each input column int [] colSum(int [][] a) { return new int [1]; } // returns the value that is the sum of the NE-SW diagonal of a // sum = a[0][a[0].length-1] + a[1][a[0].length-2] + a[2][a[0].length-3] + ... // sum = a[0][a[0].length-1-0] + a[1][a[0].length-1-1] + a[2][a[0].length-1-2] + ... // sum = ... + a[i][a[0].length-1-i] // what is the dimension of the array you return? // Use a single loop int diagSum1(int [][] a) { return -1; } // returns the value that is the sum of the NW-SE diagonal of a // sum = a[0][0] + a[1][1] + ... // what is the dimension of the array you return? // Use a single loop int diagSum2(int [][] a) { return -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 boolean isMagicSquare(int [][] a) { return false; } // insert a row at index n // place 0 at each inserted element // Assume a is rectangular // return a new array 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 // return a new array int [][] insertColumn(int[][] a, int n) { return new int[1][1]; } // delete a row at index n // Assume a is rectangular // return a new array int [][] deleteRow(int[][] a, int n) { return new int[1][1]; } // delete a column at index n // Assume a is rectangular // return a new array int [][] deleteColumn(int[][] a, int n) { return new int[1][1]; } // rotate a clockwise // Assume a is rectangular // where does the (i,j) element in the new array come from? // Hint: look at the corners, then the edges, a row, a column, then a general element // return a new array int [][] rotate(int [][] a) { return new int [1][1]; } // fills a triangular array with the integers in a serpentine pattern (Hardest) // serpentine(4): // 1 2 6 7 // 3 5 8 // 4 9 // 10 int [][] serpentine(int n) { return new int[1][1]; } ```

### Deliverables

Send me your completed .pde file by the due date.

Have Fun!