Question - University of Texas at Austin



Answer the following questions:

1. Consider the following 2D array declarations. For each declaration state the number of rows, the number of columns, and the number of elements in the resulting matrix:

int[][] mat1 = new int[10][5];

int[][] table = new int[4][12];

double[][] values = new double[3][7];

String[][] names = new String[5][3];

int[][] board = {{0, 0, 1, 2},

{0, 2, 4, 5},

{0, 3, 4, 5},

{1, 0, 0 , 0}};

2. Given the declarations above what is the value of the following elements in the 2D arrays?

mat1[0][0]

table[4][10]

values[3][10]

names[1][2]

board[1][1]

3. Consider the following code segment:

int[][] mat = new int[4][3];

for(int r = 0; r < mat.length; r++)

{ for(int c = 0; c < mat[0].length; c++)

{ mat[r][c] = r + c;

}

}

Draw the resulting matrix.

4. Consider the following code segment:

int[][] mat = new int[2][3];

for(int r = 0; r < mat.length; r++)

{ for(int c = 0; c < mat[0].length; c++)

{ mat[r][c] = c - r;

}

}

Draw the resulting matrix.

5. Consider the following code segment:

int[][] mat = new int[3][4];

for(int c = 0; c < mat[0].length; c++)

{ for(int r = 0; r < mat.length; r++)

{ mat[r][c] = r * c;

}

}

Draw the resulting matrix.

6. Consider the following code segment:

int[][] mat = {{5, 3, 7},

{2, 5, 13},

{2, 1, 5},

{11, 7, 19}};

for(int r = 1; r < mat.length; r++)

{ for(int c = 1; c < mat[0].length; c++)

{ mat[r][c] = mat[r][c] - mat[r-1][c-1];

}

}

Draw the resulting matrix.

7. Complete the following method:

public boolean bingoRow(boolean[][] bingoCard)

{ /* pre: bingoCard != null, bingoCard.length > 0,

bingoCard is a square Matrix, the number of columns in each row is equal to the number of rows

(bingoCard.length == bingoCard[0].length)

post: return true if all elements of any row in

bingoCard are true, false otherwise

*/

8. Complete the following method.

public boolean bingoColumn(boolean[][] bingoCard)

{ /* pre: bingoCard != null, bingoCard.length > 0,

bingoCard is a square Matrix, the number of columns in each row is equal to the number of rows

(bingoCard.length == bingoCard[0].length)

post: return true if all elements of any column in

bingoCard are true, false otherwise

*/

9. Complete the following method.

public boolean bingoDiagonal(boolean[][] bingoCard)

{ /* pre: bingoCard != null, bingoCard.length > 0,

bingoCard is a square Matrix, the number of columns in each row is equal to the number of rows

(bingoCard.length == bingoCard[0].length)

post: return true if all elements of either of the main diagonals in bingoCard are true, false otherwise

*/

10. Complete the following method.

public boolean bingoCorners(boolean[][] bingoCard)

{ /* pre: bingoCard != null, bingoCard.length > 0,

bingoCard is a square Matrix, the number of columns in each row is equal to the number of rows

(bingoCard.length == bingoCard[0].length)

post: return true if all 4 corners of bingoCard are true, false otherwise

*/

11. Explain what the following method accomplishes.

public int[][] mystery(int[][] mat, int val)

{ //pre: mat != null, mat is a rectangular matrix

int[][] result = new int[mat.length * val][mat[0].length * val];

for(int r = 0; r < result.length; r++)

{ for(int c = 0; c < result[0].length; c++)

{ result[r][c] = mat[r / val][c / val];

}

}

return result;

}

12. Explain what the following method accomplishes.

public int[][] mystery(int[][] mat, int val)

{ //pre: mat != null, mat is a rectangular matrix

int result = 0;

for(int r = 0; r < result.length; r++)

for(int c = 0; c < result[0].length; c++)

if(mat[r][c] == val)

result++;

return result;

}

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download