Given the following code int x new int 3 3 int sum 15 fo

Given the following code, int [] [] x = new int [3] [3]; int sum = 15; for (inc r = x.length-1;r> = 0;r--) {for (int c = x[r].length - 1; c>= 0; c^--) {sum+ =c; x[r] [c] = sum;}} will the contents of the two-dimensional array be after executing the above your result in the 3 times 3 table below

Solution

int[][] x = new int[3][3];
int sum = 15;
for(int r = x.length-1; r>= 0; r--)       //This loop starts at 2, and will go down till 0.
{
for(int c = x[r].length - 1; c >= 0; c--)   //This loops also starts at 2, and will go down till 0.
{
sum += c;                           //column value will be added to sum.
x[r][c] = sum;                       //And the particular row,column index is updated.
}
}
//So, the loop runs for values:
//2, 2   = Here sum value is updated to sum += 2, so sum = 17.
//2, 1   = Here sum value is updated to sum += 1, so sum = 18.
//2, 0   = Here sum value is updated to sum += 0, so sum = 18.
//
//1, 2   = Here sum value is updated to sum += 2, so sum = 20.
//1, 1   = Here sum value is updated to sum += 1, so sum = 21.
//1, 0   = Here sum value is updated to sum += 0, so sum = 21.
//
//0, 2   = Here sum value is updated to sum += 2, so sum = 23.
//0, 1   = Here sum value is updated to sum += 1, so sum = 24.
//0, 0    = Here sum value is updated to sum += 0, so sum = 24.

So, finally, the matrix values are:

24 24 23
21 21 20
18 18 17
 Given the following code, int [] [] x = new int [3] [3]; int sum = 15; for (inc r = x.length-1;r> = 0;r--) {for (int c = x[r].length - 1; c>= 0; c^--) {s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site