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

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

LANGUAGE: JAVA

CHALLENGE:

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

SOLUTION:

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

8 comments

Add comment