Assume you are given an int variable named sum and a 2-dimensional array of ints that has been created and assigned to a2d. Write some statements that compute the sum of all the elements in the entire 2-dimensional array and assign the value to sum.

1
Assume you are given an int variable named sum and a 2-dimensional array of ints that has been created and assigned to a2d. Write some statements that compute the sum of all the elements in the entire 2-dimensional array and assign the value  to sum.

LANGUAGE: JAVA

CHALLENGE:

Assume you are given an int variable named sum and a 2-dimensional array of ints that has been created and assigned to a2d. Write some statements that compute the sum of all the elements in the entire 2-dimensional array and assign the value to sum.

SOLUTION:

sum = 0;
for (int i=0; i < a2d.length; i++)
for (int j=0; j < a2d[i].length; j++)
sum += a2d[i][j];

One comment

Add comment