Assume you are given an int variable named nPositive and a two-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 two-dimensional array that are greater than zero and assign the value to nPositive.

Answers

Answer 1

Answer:

public class Main

{

public static void main(String[] args) {

    int nPositive = 0;

    int[][] a2d = {{-7,28, 92}, {0,11,-55}, {109, -25, -733}};

 for (int i = 0; i < a2d.length; i++) {

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

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

             nPositive++;

                       }

                  }

 }

 System.out.println(nPositive);

}

}

Explanation:

*The code is in Java.

Initialize the nPositive as 0

Initialize a two dimensional array called a2d

Create a nested for loop to iterate through the array. If an element is greater than 0, increment the nPositive by 1

When the loop is done, print the nPositive

Answer 2

Answer:

const a2d =  [[7,28, 92], [0,11,-55], [109, -25, -733]];

let nPositive = a2d.reduce((a,c) => a + c.filter(n => n>0).length, 0);

console.log(nPositive);

Explanation:

Just for fun, I want to share the solution in javascript when using the powerful list operations. It is worthwhile learning to understand function expressions and the very common map(), reduce() and filter() primitives!


Related Questions

To build a user interface that contains graphical components, the components ____. must each be added to a separate panel. must be added directly to a frame component. must be added to a panel that is contained within a frame. must be added to a frame that is contained within a panel.

Answers

Answer:

To build a user interface that contains graphical components, the components must be added to a panel that is contained within a frame.

Explanation:

Other Questions
Maurice shot 2 under par, or -2, on each of the first 4 holes of golf. What is his score with respect to par after the fourth hole? What decimal represents the shaded part in the picture? What's the value of this Python expression? ((10 >= 5*2) and (10 How is copying line segment similar to copying an angle? If (x+3) varies directly as y and x=3 when y=12, what is the value of x when y=8? ARE FINS LIMBS?? GOOD ANSWER OR ELSE I WILL REPORT YOU. A car has a mass of 1200 kg and an acceleration of 4 m/s^2. If the friction on the car is 200 N, how much force is the thrust providing? The terminal window wants to evaluate your current bash knowledge by using the ~/workspace/nested-directories folder: a. cd into the nested-directories/nested-level-1/ directory by using an absolute path b. cd into the nested-level-3/ directory by using a relative path d. Move the entire ~/workspace/config/ directory to the nested-level-1/ directory A motorcyclist changes his speed from 20 km / h to 100 km / h in 3 seconds, maintaining a constant acceleration in that time interval. If the mass of the motorcycle is 200 kg and that of its rider is 80 kg, what is the value of the net force to accelerate the motorcycle? Help! Which activity is part of managing water resourcesstudying water to see whether it is politestaking a long hot bath to say warmdrinking water and using it for swimming and boatingtransporting water to areas where it is needed Will mark brainliest for the first person who answered correctly Which of the following is an example of non-renewable energy? Hydroelectric Gas Solar Wind Which of these viral cytopathic effects is most likely to be associated with the development of cancer? To plot the point (5, -3) you would move up 5, then left 3.TrueFalse The mass of Box A and Box B is 0.6 kg. The mass of Box A and Box C is 1.3 kg.Box C is 3 times as heavy as Box B. Find the mass of Box A. And the Soul Shall Dance is described as a very realistic play due to the details and focus on objects by playwright, Wakako Yamauchi. What are three of these details and objects how many are 4 x 4 ? Purple paint requires an exact mixture of blue and red. The ratio of blue paint to red paint is 2:5. Marissa needs 35 cups of purple paint to paint a wall. How much of each color would be needed to make a total of 35 cups of perfect purple paint? how to do this question plz Pqrs is a rhombus. Find x, y and z. Justify your finding. This commentary is from the question paper, 9093/12/O/N/18, please go through my commentary and tell me how to improve i really am struggling with this part of English. My exams are coming soon, please help me. Anyone, criticism would be much appreciated