Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java: Suppose the input is 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4. What is the outpu

ID: 3577868 • Letter: I

Question

In Java:

Suppose the input is 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4. What is the output of the following code?

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

   

    double[][] m = new double[4][4];

   

    for (int i = 0; i < 4; i++)

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

        m[i][j] = input.nextDouble();

    System.out.print(ttt(m));

}

public static int ttt(double[][] m) {

    int sum = 0;

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

        sum += m[i][i];

    return sum;

}

}

Explanation / Answer

output:

10.

The array is

1 2 3 4

1 2 3 4

1 2 3 4

1 2 3 4

Here in this program all diagonal elements of two dimensional array m[4][4] are added. m[0][0] +m[1][1] +m[2][2]+m[3][3] = 1+2+3+4 =10

import java.util.Scanner;

class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

double[][] m = new double[4][4];

for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
m[i][j] = input.nextDouble();

System.out.print(ttt(m));
}

public static int ttt(double[][] m) {
int sum = 0;

for (int i = 0; i < m.length; i++)
{
sum += m[i][i];
System.out.println(m[i][i]); //add diagonal elements
}

return sum;
}
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote