C# 程式說明了下三角矩陣


對於下三角矩陣,將主對角線以上的所有元素設為零。

設定以下條件 −

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("
Enter 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("
Lower Triangular Matrix ");       for (i = 0; i < m; i++) {          for (j = 0; j < n; j++) {             Console.Write(A[i, j] + "\t");          }          Console.WriteLine();       }       for (i = 0; i < m; i++) {          Console.Write("
");          for (j = 0; j < 3; j++) {             if (i >= j)                Console.Write(A[i, j] + "\t");             else                Console.Write("0\t");          }       }       Console.ReadLine();    } }

輸出

Enter number of rows and columns of the matrix  
Enter matrix elements:  
Lower Triangular Matrix 

更新於:19-6-2020

230 次瀏覽

職業啟航

完成課程即可獲得認證

開始學習
廣告