0% found this document useful (0 votes)
8 views3 pages

10b. WS Some Matrix Review Extra Practice U4a U4b SOL

Uploaded by

freeve4handcgame
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

10b. WS Some Matrix Review Extra Practice U4a U4b SOL

Uploaded by

freeve4handcgame
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

AP CSA WS: Some Matrix Review Problems Name___________________

1.) Consider the following method: If a two-dimensional array named


numbers is initialized to store the
public static void mystery2d(int[][] a) { following integers, what are its
for (int r = 0; r < a.length; r++) { contents after the call shown?
for (int c = 1; c < a[0].length; c++) int[][] numbers =
{ { {9, 6, 18, 6 },
if (a[r][c - 1] > a[r][c]) { {7, 4, 2, 9 },
a[r][c] = a[r][c - 1]; {11, 6, 1, -8} };
}
} mystery2d(numbers);
}
}

2.) Assume that a two-dimensional rectangular array of integers called data has been
declared with four rows and seven columns. Write a for loop to initialize the third row of
data to store the odd numbers 29 through 17. Then write another for loop to initialize the
last column of numbers to all be 17.
17

17

29 27 25 23 21 19 17
17

3.): Show how the matrix changes after the code is executed.
int[][] matrix = new int[5][5];
for(int r = 0; r < matrix.length; r++) {
for(int c = 4; c >= 0; c--) {
if(c == 4 || r == 0)
matrix[r][c] = 2;
else
matrix[r][c] = matrix[r][c + 1] + matrix[r - 1][c];
}
}

# 4: What are the contents of the matrix after the code is run?
String str = "adrien";
int size = str.length();
char[][] matrix = new char[size][size];

for(int k = 0; k < size; k++ )


{
matrix[0][k] = str.charAt(size-1-k);
matrix[k][0] = str.charAt(size-1-k);
matrix[size - 1][k] = str.charAt(k);
matrix[k][size - 1] = str.charAt(k);
matrix[k][size / 2] = str.charAt(size / 2);
}
#5: Use loops to fill in the diagonal of this matrix with the word "bert".
Then use another set of loops to fill in the other diagonal with the word
"ernie." Create the matrix before writing the loops.

"ernie"

"bert" "ernie"

"bert" "ernie"

"ernie" "bert"

"ernie" "bert"
Answers: WS: Some Matrix Review Problems
// Exercise #1: // Exercise #3:
{ {9, 9, 18, 18}, { { 2, 2, 2, 2, 2 },
{7, 7, 7, 9}, { 10, 8, 6, 4, 2 },
{ 11, 11, 11, 11 } } { 30, 20, 12, 6, 2 },
{ 70, 40, 20, 8, 2 },
{ 140, 70, 30, 10, 2 } }

// Exercise #2: // Exercise #4:


int[][] data = new int[4][7]; { { 'n', 'e', 'i', 'r', 'd', 'a' },
for ( int r = 0; r < data.length; r++ ) { 'e', '' , '', 'i', '', 'd' },
{ 'i', '' , '', 'i', '', 'r' },
{
{ 'r', '' , '', 'i', '', 'i' },
data[ r ][ data[r].length - 1 ] = 17; { 'd', '' , '', 'i', '', 'e' },
} { 'a', 'd', 'r', 'i', 'e', 'n' } }

int num = 29; //----------------------------------------------------------------


for ( int c = 0; c < data[2].length; c++ )
{
data[ 2 ][ c ] = num;
num -= 2;
}

// Exercise #5:

String[][] mat = new String[ 5 ][ 6 ];

int r = 1, c = 0;
while ( r < mat.length )
{
mat[ r ][ c ] = "bert";
r++;
c++;
}

r = 0;
c = mat[ 0 ].length - 2;
while ( r < mat.length )
{
mat[ r ][ c ] = "ernie";
r++;
c--;
}

You might also like