Diagonal matrix in java

Asked

Viewed 1,986 times

5

First, I would like to inform you that I am new here on the site, and if by chance my question was left without information or badly organized please speak.

I was trying to solve that question:

Fotografia do exercício

I managed to develop this code:

import java.util.Scanner;

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

    int N = ler.nextInt();



    int i, j, m[][] = new int[N][N];

    // Ler matriz   
    for (i=0; i<N; i++) {
      //informa linha
      System.out.printf("Informe os elementos %da. linha:\n", (i+1));
          for (j=0; j<N; j++) {
            //informa qual numero deve se colocar
            System.out.printf("m[%d][%d] = ", i, j);
            m[i][j] = ler.nextInt();
          }
      // salta linha para n ficar confuso
      System.out.printf("\n");
    }

But I can not understand how I will take the differences of the diagonals of the matrix.

2 answers

3


Stayed like this:

import java.util.Scanner;

public class Matriz1 {

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

        int n = ler.nextInt();

        int matriz[][] = new int[n][n];

        // Lê a matriz.
        for (int i = 0; i < n; i++) {
            // Informa a linha.
            System.out.printf("Informe os elementos %da. linha:\n", (i + 1));
            for (int j = 0; j < n; j++) {
                // Informa qual número deve se colocar.
                System.out.printf("m[%d][%d] = ", i, j);
                matriz[i][j] = ler.nextInt();
            }
            // Salta linha para não ficar confuso.
            System.out.printf("\n");
        }

        // Obtém a soma das diagonais.
        int somaDiagonal1 = 0, somaDiagonal2 = 0;
        for (int i = 0; i < n; i++) {
            somaDiagonal1 += matriz[i][i];
            somaDiagonal2 += matriz[n - 1 - i][i];
        }

        // Calcula a diferença das somas.
        int diferenca = somaDiagonal1 - somaDiagonal2;
        if (diferenca < 0) diferenca *= -1;

        // Exibe o resultado.
        System.out.printf("A difereça da soma das diagonais é %d.", diferenca);
    }
}

2

I’m not an expert in java, but I figured that might help you, add this to your code:

int D = 0; // soma dos números da diangonal 2

for (i=0; i<N; i++) {
  D += m[i][i];
}

int d = 0; // somas dos números da diangonal 1

for (i=0; i<N; i++) {

  for (j=N-1; j>=0; j--) {
     d += m[i][j];
     break;
  }
}
int df = d - D; // resultado da diferença da soma dos números das diagonais (o que voce quer :D)

Once I did something similar in javascript, I used the same logic, if something goes wrong, please let me know.

  • D is working but d seems not to be decreasing j because in the example 11 02 04 04 05 06 10 08 -12 D=4 d=-2 I think d is doing 4+6+-12 ps:I put 0 in the numbers less than 10 for easy reading I did not put so in the program

Browser other questions tagged

You are not signed in. Login or sign up in order to post.