Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để hiển thị ma trận tam giác trên

Trong bài này, chúng ta sẽ hiểu cách hiển thị một ma trận tam giác trên. Ma trận có sự sắp xếp theo hàng và cột của các phần tử của nó. Một ma trận có m hàng và n cột có thể được gọi là ma trận m × n. Ma trận tam giác trên là ma trận tam giác với tất cả các phần tử bên dưới đường chéo chính là 0.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

The matrix is defined as:
2 1 4
1 2 3
3 6 2

Đầu ra mong muốn sẽ là -

The upper triangular matrix is:
2 1 4
0 2 3
0 0 2

Thuật toán

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, assign 0 to all the [i][j] positions that comes below the diagonal of the matrix using rows != column condition.
Step 5 - Display the matrix as result
Step 6 - Stop

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "main".

public class UpperTriangle {
   public static void upper_triangular_matrix(int input_matrix[][]) {
   }
   public static void main(String[] args) {
      int input_matrix[][] = {
         { 2, 1, 4 },
         { 1, 2, 3 },
         { 3, 6, 2 }
      };
      int rows = input_matrix.length;
      int column = input_matrix[0].length;
      System.out.println("The matrix is defined as: ");
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < column; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      if (rows != column) {
         return;
      } else {
         for (int i = 0; i < rows; i++) {
            for (int j = 0; j < column; j++) {
               if (i > j) {
                  input_matrix[i][j] = 0;
               }
            }
         }
         System.out.println("\nThe upper triangular matrix is: ");
         for (int i = 0; i < rows; i++) {
            for (int j = 0; j < column; j++) {
               System.out.print(input_matrix[i][j] + " ");
            }
            System.out.println();
         }
      }
   }
}

Đầu ra

The matrix is defined as:
2 1 4
1 2 3
3 6 2

The upper triangular matrix is:
2 1 4
0 2 3
0 0 2

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

public class UpperTriangle {
   public static void upper_triangular_matrix(int input_matrix[][]) {
      int rows = input_matrix.length;
      int column = input_matrix[0].length;
      if (rows != column) {
         return;
      } else {
         for (int i = 0; i < rows; i++) {
            for (int j = 0; j < column; j++) {
               if (i > j) {
                  input_matrix[i][j] = 0;
               }
            }
         }
         System.out.println("\nThe upper triangular matrix is: ");
         for (int i = 0; i < rows; i++) {
            for (int j = 0; j < column; j++) {
               System.out.print(input_matrix[i][j] + " ");
            }
            System.out.println();
         }
      }
   }
   public static void main(String[] args) {
      int input_matrix[][] = {
         { 2, 1, 4 },
         { 1, 2, 3 },
         { 3, 6, 2 }
      };
      int rows = input_matrix.length;
      int column = input_matrix[0].length;
      System.out.println("The matrix is defined as: ");
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < column; j++) {
            System.out.print(input_matrix[i][j] + " ");
         }
         System.out.println();
      }
      upper_triangular_matrix(input_matrix);
   }
}

Đầu ra

The matrix is defined as:
2 1 4
1 2 3
3 6 2

The upper triangular matrix is:
2 1 4
0 2 3
0 0 2