Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #9 Multi-Dimensional Arrays
What are Multi-Dimensional Arrays? The preceding chapter introduced how to use one-dimensional arrays to store linear collections of elements. You can use a two-dimensional array to store a matrix or a table. For example, the following table that lists the distances between cities can be stored using a two-dimensional array named distances. double[][] distances = { {0, 983, 787, 714, 1375, 967, 1087, {983, 0, 214, 1102, 1763, 1723, 1842, {787, 214, 0, 888, 1549, 1548, 1627, {714, 1102, 888, 0, 661, 781, 810, {1375, 1763, 1549, 661, 0, 1426, 1187, {967, 1723, 1548, 781, 1426, 0, 239, {1087, 1842, 1627, 810, 1187, 239, 0, ; Declaring and creating Variables of Two-Dimensional Arrays Multi-Dimensional array is an array of arrays. For example, to declare two-dimensional array, use the following syntax: int[][] matrix; To initialize the matrix, use the new keyword. Example: create a new matrix with 5 rows and 5 columns: 2
int[][] matrix = new int[5][5]; The previous code will create the matrix and fill it with the default value: 0. To assign the value 7 to a specific element at row 2 and column 1, you can use the following syntax: matrix[2][1] = 7; The matrix will be represented in memory as array of arrays, as following: You can also use an array initializer to declare, create, and initialize a two-dimensional array. 3
Obtaining the Lengths of Two-Dimensional Arrays A two-dimensional array is actually an array in which each element is a one-dimensional array. The length of an array x is the number of elements in the array, which can be obtained using x.length. x[0], x[1],..., and x[x.length-1] are arrays. Their lengths can be obtained using x[0].length, x[1].length,..., and x[x.length-1].length For example, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional arrays and each contains four elements. x.length is 3, and x[0].length, x[1].length, and x[2].length are 4. Ragged Arrays Each row in a two-dimensional array is itself an array. Thus, the rows can have different lengths. An array of this kind is known as a ragged array. Here is an example of creating a ragged array: 4
If you don t know the values in a ragged array in advance, but do know the sizes say, the same as before you can create a ragged array using the following syntax: int[][] trianglearray = new int[5][]; trianglearray[0] = new int[5]; trianglearray[1] = new int[4]; trianglearray[2] = new int[3]; trianglearray[3] = new int[2]; trianglearray[4] = new int[1]; Processing Two-Dimensional Arrays Nested for loops are often used to process a two-dimensional array. Suppose an array matrix is created as follows: int[][] matrix = new int[10][10]; The following are some examples of processing two-dimensional arrays. Initializing arrays with input values. int[][] matrix = new int[10][10]; Scanner input = new Scanner(System.in); System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: "); for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = input.nextint(); Printing arrays: int[][] matrix = new int[10][10]; for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { System.out.print(matrix[row][column] + " "); System.out.println(); 5
Summing all elements. int[][] matrix = new int[10][10]; int total = 0; for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++){ total += matrix[row][column]; Summing elements by column. for (int column = 0; column < matrix[0].length; column++) { int total = 0; for (int row = 0; row < matrix.length; row++) total += matrix[row][column]; System.out.println("Sum for column " + column + " is " + total); Random shuffling int[][] matrix = new int[10][10]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { int i1 = (int) (Math.random() * matrix.length); int j1 = (int) (Math.random() * matrix[i].length); int temp = matrix[i][j]; matrix[i][j] = matrix[i1][j1]; matrix[i1][j1] = temp; Passing Two-Dimensional Arrays to Methods You can pass a two-dimensional array to a method just as you pass a one-dimensional array. You can also return an array from a method. When passing a two-dimensional array to a method, the reference of the array is passed to the method. 6
Ex: Write a method to add two matrices. The header of the method is as follows: public static double[][] addmatrix(double[][] a, double[][] b) Solution: public static double[][] addmatrix(double[][] m1, double[][] m2) { double[][] result = new double[m1.length][m1[0].length]; for (int i = 0; i < result.length; i++) for (int j = 0; j < result[i].length; j++) result[i][j] = m1[i][j] + m2[i][j]; return result; Multidimensional Arrays A two-dimensional array consists of an array of one-dimensional arrays and a threedimensional array consists of an array of two-dimensional arrays. double[][][] scores = new double[6][5][2]; You can also use the short-hand notation to create and initialize the array as follows: double[][][] scores = { {{7.5, 20.5, {9.0, 22.5, {15, 33.5, {13, 21.5, {15, 2.5, {{4.5, 21.5, {9.0, 22.5, {15, 34.5, {12, 20.5, {14, 9.5, {{6.5, 30.5, {9.4, 10.5, {11, 33.5, {11, 23.5, {10, 2.5, {{6.5, 23.5, {9.4, 32.5, {13, 34.5, {11, 20.5, {16, 7.5, {{8.5, 26.5, {9.4, 52.5, {13, 36.5, {13, 24.5, {16, 2.5, {{9.5, 20.5, {9.4, 42.5, {13, 31.5, {12, 20.5, {16, 6.5; 7
Lab Work Ex1: Write two methods that return the sum of columns and rows individually and put the result in one-dimensional array for each result using the following methods header: public static int[] sumrows (int[][] array) public static int[] sumcolumns (int[][] array) Solution: public static int[] sumrows(int[][] array) { int[] result = new int[array.length]; int tempsum = 0; int i = 0; for (int row = 0; row < array.length; row++) { for (int column = 0; column <array[0].length; column++){ tempsum += array[row][column]; result[i] = tempsum; i++; tempsum = 0; return result; public static int[] sumcolumns(int[][] array) { int[] result = new int[array[0].length]; int tempsum = 0; int i = 0; for (int column = 0; column < array[0].length; column++) { for (int row = 0; row < array.length; row++) { tempsum += array[row][column]; result[i] = tempsum; i++; tempsum = 0; return result; 8
Ex2: Write a method that takes two int arrays as parameters and returns the multiplication of the two arrays. Solution: public static int[][] multiplicar(int[][] A, int[][] B) { int arows = A.length; int acolumns = A[0].length; int brows = B.length; int bcolumns = B[0].length; if (acolumns!= brows) { System.err.println("A:Rows: " + acolumns + " did not match B:Columns " + brows + "."); return null; int[][] C = new int[arows][bcolumns]; for (int i = 0; i < arows; i++) { // arow for (int j = 0; j < bcolumns; j++) { // bcolumn for (int k = 0; k < acolumns; k++) { // acolumn C[i][j] += A[i][k] * B[k][j]; return C; Ex3: In this question, you have n * n matrix as shown below, you have to replace each 0 in the matrix to the number of * around it, then print the result. char[][] input = { {'0', '0', '*', '0', '*', {'*', '*', '*', '0', '0', {'0', '*', '0', '*', '0', {'*', '0', '*', '0', '0', {'0', '*', '*', '0', '*', ; 9
2 4 * 3 * * * * 4 2 4 * 5 * 1 * 5 * 4 2 2 * * 3 * Solution: char[][] input = { {'0', '0', '*', '0', '*', {'*', '*', '*', '0', '0', {'0', '*', '0', '*', '0', {'*', '0', '*', '0', '0', {'0', '*', '*', '0', '*', ; int[][] output = new int[input.length][input[0].length]; for (int i = 0; i < input.length; i++) { for (int j = 0; j < input[0].length; j++) { if (input[i][j] == '*') { if (j > 0) output[i][j - 1]++; if (i > 0 && j > 0) output[i - 1][j - 1]++; if (i > 0) output[i - 1][j]++; if (i > 0 && j < input.length - 1) output[i - 1][j + 1]++; if (j < input.length - 1) output[i][j + 1]++; if (i < input.length - 1 && j < input.length- 1) output[i + 1][j + 1]++; if (i < input.length - 1) output[i + 1][j]++; if (i < input.length - 1 && j > 0) output[i + 1][j - 1]++; 10
for (int i = 0; i < input.length; i++) { for (int j = 0; j < input[0].length; j++) { if (input[i][j] == '*') { System.out.print('*' + " "); else System.out.print(output[i][j] + " "); System.out.println(); Homework 1. Write a method that sums all the numbers in the major diagonal in an n * n matrix of double values using the following header: public static double summajordiagonal(double[][] m) 2. Write a program that generates a 6-by-6 two-dimensional matrix filled with 0s and 1s, displays the matrix, and prints whether every row and every column have an even number of 1s or not. Good Luck 11