2D配列またはマトリックスをC#で印刷する
まず、2次元配列を設定します。
int[,] arr = new int[10, 10];
次に、ユーザーから要素を取得します-
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
arr[i, j] = Convert.ToInt16(Console.ReadLine());
}
} マトリックスを表示するための完全な例を見てみましょう。
例
using System;
using System.Linq;
class Demo {
static void Main() {
int m, n, i, j;
// rows and columns of the matrix+
m = 2;
n = 2;
int[,] arr = new int[10, 10];
Console.Write("Enter elements of the Matrix: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
arr[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
Console.WriteLine("Printing Matrix: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
} 出力
以下は出力です。
Enter elements of the Matrix: 5 10 12 15 Printing Matrix: 510 1215
-
Cプログラムで指定された配列から下三角行列パターンを印刷します。
n x nの行列が与えられた場合、タスクはnxnの行列を下三角パターンで印刷することです。 下三角行列は、主対角要素と残りの要素をゼロとして含む、主対角の下の要素を持つ行列です。 ダイアグラムを使ってこれを理解しましょう- 緑の要素の上は主対角線の下の要素であり、赤の要素は主対角線より上の要素であり、ゼロとして設定されています。 例 Input: matrix[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } Output: 1
-
C#の次元配列?
C#では多次元配列が可能です。 intの2次元配列をとして宣言します。 int [ , , ] a; 多次元配列の最も単純な形式は、2次元配列です。 2次元配列は、1次元配列のリストです。 以下は、3行4列の2次元配列です。 ここで、C#で多次元配列を操作する例を見てみましょう。 例 using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { &nb