Java

TOPIC 08 – MULTI DIMENSIONAL ARRAYS

 

LESSON WORK

 

 

QUESTION #1 – SHORT ANSWER QUESTIONS (SELF CHECK)

 

a)    Consider the following memory diagram of a multidimensional integer array.  Write the statement that will create this array.

 

b)    Consider the following memory diagram of a multidimensional integer array.  Write the statement that will create this array.  (Arrowheads were omitted to allow Mr. C. to keep his sanity when drawing this thing!)

 

 

c)     For the statement int[][][] x = new int[4][2][5]; which of the following assignment statements would cause errors?

 
          i)       
x[0][0][0] = 32;

          ii)        x[2][1][3] = 24;

          iii)       x[4][1][4] = 51;

          iv)       x[2][2][0] = 4;

          v)       x[-1][1][2] = 54;

 

 

d)    Answer the following questions related to a two-dimensional array a.

 

i)                 What are the indexes of the element at the top left corner of the array?

ii)               What are the indexes of the element at the top right corner of the array?

iii)              What are the indexes of the element at the bottom left corner of the array?

iv)             What are the indexes of the element at the bottom right corner of the array?


Click here for solutions to this question.

QUESTION 2 (SELF CHECK)


Consider the 2D array diagram below and create the program that will:

a) Create that array.

b) Give each element the value it has in the diagram by using nested for loops to go through the array.  Notice that element [r][c] has the value r+c.


c) Write the code required to output the contents of the array in a grid format such as:

0 1 2 3 4 5 6
1 2 3 4 5 6 7
2 3 4 5 6 7 8

3 4 5 6 7 8 9

 

Click here for the solution.

 

QUESTION #3

 

a)    Create a tic tac toe board that is represented by a 2D array that is 3 x 3. 

b)    Hardcode the board with “X” and “O”s using any method you wish.  No need to get too complicated here.

c)     Write the code required to output the board in an appropriate way for the game.

 

NOTE: You are not creating a Tic Tac Toe game here.

 

QUESTION #4

 

Write a function named getDiagonal that gets an int[][] and returns an int[] consisting of the values of the diagonal in the 2D array. 

 

Consider the following diagram for a 2D array.  The red numbers are the diagonal.

 

4 5 8 9 1

9 2 4 1 1

4 5 1 5 2

5 2 0 1 0

6 6 7 3 5

 

You will find the function header below.  Make sure to test your code.

 

/** Returns an array containing the elements of the diagonal

  * they appear in arr2D.

  * Precondition: arr2D has the same number of rows and columns

  * Postcondition: arr2D is unchanged.

  */

 

public static int[] getDiagonal(int[][] arr2D)

{}

 

QUESTION #5

 

Write the function isOddEvenArray() that gets an int[][] and returns true if:

·                         The sum of rows 0, 2, 4, … are all even.

·                         The sums of rows 1, 3, 5, … are all odd.

·                         The four corner elements are 0.

Otherwise, the function returns false.