Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

행렬의 대각선 합을 계산하는 Java 프로그램

<시간/>

이 기사에서는 행렬의 대각선의 합을 계산하는 방법을 이해할 것입니다. 행렬에는 요소의 행과 열 배열이 있습니다. 주대각선은 왼쪽 위 모서리에서 오른쪽 아래 모서리로 가는 정사각 행렬의 대각선입니다.

보조 대각선은 왼쪽 아래 모서리에서 오른쪽 위 모서리로 가는 정사각 행렬의 대각선입니다.

아래는 동일한 데모입니다 -

입력이 다음과 같다고 가정 -

The input matrix:
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

원하는 출력은 -

Sum principal diagonal elements: 74
Sum of secondary diagonal elements: 45

알고리즘

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using two for-loops, add the diagonal elements use the condition ‘i’ = ‘j’ to get the sum of principle diagonal elements and the condition ‘i’ + ‘j’ = matrix_size – 1 to get the sum of secondary diagonal elements.
Step 5 - Display the result
Step 5 - Stop

예시 1

여기에서 모든 작업을 'main' 기능 아래에 묶습니다.

public class MatrixDiagonals {
   static public void main(String[] args) {
      int[][] input_matrix = {
         { 4, 5, 6, 7 },
         { 1, 7, 3, 4 },
         { 11, 12, 13, 14 },
         { 23, 24, 25, 50 }
      };
      int matrix_size = 4;
      System.out.println("The matrix is defined as : ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++)
         System.out.print( input_matrix[i][j] + " ");
         System.out.print("\n");
      }
      int principal_diagonal = 0, secondary_diagonal = 0;
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            if (i == j)
               principal_diagonal += input_matrix[i][j];
            if ((i + j) == (matrix_size - 1))
               secondary_diagonal += input_matrix[i][j];
         }
      }
      System.out.println("\n The sum of principal diagonal elements of the matrix is: " + principal_diagonal);
      System.out.println("\n The sum of secondary diagonal elements of the matrix is: " + secondary_diagonal);
   }
}

출력

The matrix is defined as :
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

The sum of principal diagonal elements of the matrix is: 74

The sum of secondary diagonal elements of the matrix is: 45

예시 2

여기에서 객체 지향 프로그래밍을 나타내는 함수로 작업을 캡슐화합니다.

public class MatrixDiagonals {
   static void diagonals_sum(int[][] input_matrix, int matrix_size) {
      int principal_diagonal = 0, secondary_diagonal = 0;
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            if (i == j)
               principal_diagonal += input_matrix[i][j];
            if ((i + j) == (matrix_size - 1))
               secondary_diagonal += input_matrix[i][j];
         }
      }
      System.out.println("\n The sum of principal diagonal elements of the matrix is: " + principal_diagonal);

      System.out.println("\n The sum of secondary diagonal elements of the matrix is: " + secondary_diagonal);
   }
   static public void main(String[] args) {
      int[][] input_matrix = {
         { 4, 5, 6, 7 },
         { 1, 7, 3, 4 },
         { 11, 12, 13, 14 },
         { 23, 24, 25, 50 }
      };
      int matrix_size = 4;
      System.out.println("The matrix is defined as : ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++)
         System.out.print( input_matrix[i][j] + " ");
         System.out.print("\n");
      }
      diagonals_sum(input_matrix, matrix_size);
   }
}

출력

The matrix is defined as:
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

The sum of principal diagonal elements of the matrix is:74

The sum of secondary diagonal elements of the matrix is: 45