먼저 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