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

โปรแกรม C# แสดง Lower Triangular Matrix

Lower Triangular Matrix (เมทริกซ์สามเหลี่ยมล่าง) คือเมทริกซ์ที่สมาชิกทุกตัวที่อยู่ ด้านบนแนวทแยงมุมหลัก (Main Diagonal) เป็นศูนย์

เงื่อนไข: ถ้า i >= j แสดงค่าเดิม มิฉะนั้นแสดง 0

if (i >= j)
    Console.Write(A[i, j] + "\t");
else
    Console.Write("0\t");

ตัวอย่างโค้ดเต็ม

using System;
using System.Linq;
class Demo {
    static void Main() {
        int m, n, i, j;
        Console.Write("Enter number of rows and columns of the matrix ");
        m = Convert.ToInt16(Console.ReadLine());
        n = Convert.ToInt16(Console.ReadLine());
        int[,] A = new int[10, 10];
        Console.Write("\nEnter matrix elements: ");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                A[i, j] = Convert.ToInt16(Console.ReadLine());
            }
        }
        Console.Clear();
        Console.WriteLine("\nLower Triangular Matrix ");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                if (i >= j)
                    Console.Write(A[i, j] + "\t");
                else
                    Console.Write("0\t");
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

ตัวอย่าง: เมทริกซ์ 3x3

1 2 3     →    1 0 0
4 5 6     →    4 5 0
7 8 9     →    7 8 9