Which of the following statements assigns the letter S to the third row and first column of a two-dimensional array named strGrid (assuming row-major order).
The code letterGrid[0][2] = "S"; actually sets the 1st row and 3rd column to hold a reference to the String object "S".
strGrid[1][3] = "S";
This would be true if row and column indices started at 1 instead of 0 and if this was in column major order.
strGrid[3][1] = "S";
This would be true if row and column indices started at 1 instead of 0.
strGrid[2][0] = "S";
In row-major order the row is specified first followed by the column. Row and column indices start with 0. So letterGrid[2][0] is the 3rd row and 1st column.
strGrid[0][0] = "S";
This would set the element at the first row and column.
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}
4
This would be correct if the variable col was 0 because then it would add 1 + 1 + 1 + 1 which is 4.
8
Since col is matrix[0].length - 2 it is 4 - 2 which is 2. This code will loop through all the rows and add all the numbers in the third column (index is 2) which is 2 + 2 + 3 + 1 which is 8.
9
This would be correct if the variable col was 1 because then it would add 1 + 2 + 2 + 4 which is 9.
12
This would be correct if the variable col was 3 becuase then it would add 2 + 4 + 4+ 2 which is 12.
10
This would be true if we were adding the values in the 3rd row (row = 2) instead of the 3rd column. This would be 1 + 2 + 3 + 4 which is 10.
int [][] mat = new int [4][3];
for (int row = 0; row < mat.length; row++)
{
for (int col = 0; col < mat[0].length; col++)
{
if (row < col)
mat[row][col] = 1;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 3;
}
}
{ {2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}}
This woud be true if the code put a 3 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 1 in the array when the row index is greater than the column index.
{ {2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}}
This code will put a 1 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 3 in the array when the row index is greater than the column index.
{ {2 1 1 1}, {3 2 1 1}, {3 3 2 1}}
This code creates a 2D array with 4 rows and 3 columns so this canβt be right.
{ {2 3 3 3}, {1 2 3 3}, {1 1 2 3}}
This code creates a 2D array with 4 rows and 3 columns so this canβt be right.
{ {1 1 1 1}, {2 2 2 2}, {3 3 3 3}}
This code creates a 2D array with 4 rows and 3 columns so this canβt be right.
int[][] arr = { {3,2,1},{1,2,3}};
for (int row = 1; row < arr.length; row++)
{
for (int col = 1; col < arr[0].length; col++)
{
if (arr[row][col] % 2 == 1)
{
arr[row][col] = arr[row][col] + 1;
}
if (arr[row][col] % 2 == 0)
{
arr[row][col] = arr[row][col] * 2;
}
}
}
{ {6, 4, 2}, {2, 4, 6}}
Check the starting values on the nested loops.
{ {3, 2, 1}, {1, 4, 6}}
Notice that there are two ifβs, not an if and else.
{ {3, 2, 1}, {1, 4, 8}}
The first if will change an odd number to an even. The second if will also execute after an odd number has been made even. Both loops start at index 1 so this only changes the items in the second row and second and third column.
{ {4, 4, 2}, {2, 4, 4}}
Both ifβs will execute. Also, check the bounds on the nested loop.
{ {3, 2, 1}, {2, 4, 4}}
Both ifβs will execute. Check the bounds on the inner loop. When does it stop?
A two-dimensional array, imagePixels, holds the brightness values for the pixels in an image. The brightness can range from 0 to 255. What does the following method compute?
public int findMax(int[][] imagePixels)
{
int r, c;
int i, iMax = 0;
for (r = 0; r < imagePixels.length; r++)
{
for (c = 0; c < imagePixels[0].length; c++)
{
i = imagePixels[r][c];
if (i > iMax)
iMax = i;
}
}
return iMax;
}
The maximum brightness value for all pixels in imagePixel
The method works by scanning all the pixels in imagePixels and comparing them to the current iMax value. If the current is greater, it replaces iMax and becomes the new maximum brightness. This is the value that is returned.
The column with the greatest brightness sum
This could be accomplished by adding the brightness in the second loop and comparing the sum to iMax after the second loop finishes and before the first loop starts again.
The most frequent brightness value in imagePixels
To do this you would need a third loop and an array, 256 in size. In the second loop you would track how many pixels of a certain brightness had occurred using, countBright[i]++, and then in the third loop find the item in countBright with the highest value.
The row with the greatest brightness sum
Firstly, you would need to traverse the 2D array in the opposite order, going through the rows instead of the columns. Then, you would sum each rowβs brightness in the second loop and compare it to the max in the first loop.
The sum of the total brightness of imagePixels
This would be accomplished by instead of having an if statement to track the currentmax, simply using, sum += imagePixels[r][c];