Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that you have a variable twoD that refers to a two-dimensional array of i

ID: 3632775 • Letter: A

Question

Assume that you have a variable twoD that refers to a two-dimensional array of integers. For example, here is one example of such an array:
int[][] twoD = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
a) Write an assignment statement that replaces the value of 7 in the above array with a value of 15.

b) Write a code fragment that prints the values in the leftmost column of the array to which twoD refers. Print each value on its own line. For the array above, the following values would be printed:
1
5
9
Make your code general enough to work on any two-dimensional array named twoD that has at least one value in each row.

c) Write a code fragment that prints the values in the bottom row of the array to which twoD refers. Print each value on its own line. For the array above, the following values would be printed:
9
10
11
12
Make your code general enough to work on any two-dimensional array named twoD.

Explanation / Answer

please rate - thanks

Assume that you have a variable twoD that refers to a two-dimensional array of integers. For example, here is one example of such an array:
int[][] twoD = {{1, 2, 3, 4},                row 0
{5, 6, 7, 8},                                      row 1
{9, 10, 11, 12}};                                row 2
a) Write an assignment statement that replaces the value of 7 in the above array with a value of 15.

twoD[1][2]=15;

b) Write a code fragment that prints the values in the leftmost column of the array to which twoD refers. Print each value on its own line. For the array above, the following values would be printed:
1
5
9
Make your code general enough to work on any two-dimensional array named twoD that has at least one value in each row.

for(int i=0;i<twoD.length;i++)
     System.out.println(twoD[i][0]);

c) Write a code fragment that prints the values in the bottom row of the array to which twoD refers. Print each value on its own line. For the array above, the following values would be printed:
9
10
11
12
Make your code general enough to work on any two-dimensional array named twoD.

for(int i=0;i<twoD[twoD.length-1].length;i++)
     System.out.println(twoD[twoD.length-1][i]);