191k views
0 votes
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.

1 Answer

1 vote

Answer:

Here's an example code that computes the number of all the elements in the entire 2-dimensional array that are greater than zero and assigns the value to nPositive:

Step-by-step explanation:

int nPositive = 0; // initialize nPositive to zero

// create and initialize 2D array

int a2d[3][4] = {{1, 2, 3, 4},

{5, 6, -1, -2},

{0, -3, 7, -4}};

// loop through each element in the array and count positive elements

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 4; j++) {

if (a2d[i][j] > 0) {

nPositive++; // increment nPositive if element is greater than zero

}

}

}

// output the total number of positive elements in the 2D array

cout << "The total number of positive elements in the 2D array is " << nPositive << endl;

In this example, we first initialize the variable nPositive to zero. We then create and initialize a 2D array a2d with three rows and four columns.

Next, we loop through each element in the array using nested for loops, and check if the current element is greater than zero. If the element is greater than zero, we increment the variable nPositive.

Finally, we output the total number of positive elements in the 2D array using cout. In this example, the output will be:

The total number of positive elements in the 2D array is 7

Note that the specific values in the array and the dimensions of the array may vary depending on the context of the problem.

Aɳʂɯҽɾҽԃ Ⴆყ ɠσԃKEY ꦿ

User VineFreeman
by
7.6k points