Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java แสดง Upper Triangular Matrix


ในบทความนี้ เราจะเรียนรู้วิธีการแสดง Upper Triangular Matrix เมทริกซ์มีการจัดเรียงองค์ประกอบเป็นแถวและคอลัมน์ เมทริกซ์ที่มี m แถวและ n คอลัมน์ เรียกว่าเมทริกซ์ m × n Upper Triangular Matrix คือ เมทริกซ์สามเหลี่ยมที่องค์ประกอบทั้งหมดที่อยู่ใต้เส้นทแยงมุมหลัก (main diagonal) มีค่าเป็น 0

ตัวอย่างการทำงาน

สมมติข้อมูลนำเข้า (Input) คือ:

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

ผลลัพธ์ที่ต้องการ (Output) คือ:

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

อัลกอริทึม

ขั้นตอนที่ 1 - เริ่มต้น
ขั้นตอนที่ 2 - ประกาศเมทริกซ์จำนวนเต็ม input_matrix
ขั้นตอนที่ 3 - กำหนดค่าให้กับเมทริกซ์
ขั้นตอนที่ 4 - วนลูปผ่านแต่ละองค์ประกอบด้วย for-loop 2 ชั้น กำหนดค่า 0 ให้ตำแหน่ง [i][j] ที่อยู่ใต้เส้นทแยงมุม (i > j)
ขั้นตอนที่ 5 - แสดงเมทริกซ์ผลลัพธ์
ขั้นตอนที่ 6 - จบการทำงาน

ตัวอย่างที่ 1: เขียนโค้ดทั้งหมดในฟังก์ชัน 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(); 
            } 
        } 
    } 
}

ผลลัพธ์

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

ตัวอย่างที่ 2: ใช้ฟังก์ชันแยก (Object-Oriented Style)

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); 
    } 
}

ผลลัพธ์

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