jrliv
6/2/2017 - 1:31 AM

Example of how to read a multidimensional array from the console. From Fundamentals of Computer Programming with C# http://www.introprogramm

Example of how to read a multidimensional array from the console. From Fundamentals of Computer Programming with C# http://www.introprogramming.info/wp-content/uploads/2013/07/Books/CSharpEn/Fundamentals-of-Computer-Programming-with-CSharp-Nakov-eBook-v2013.pdf

Console.Write("Enter the number of the rows: ");
int rows = int.Parse(Console.ReadLine());

Console.Write("Enter the number of the columns: ");
int cols = int.Parse(Console.ReadLine());

int[,] matrix = new int[rows, cols];

Console.WriteLine("Enter the cells of the matrix:");

for (int row = 0; row < rows; row++)
{
  for (int col = 0; col < cols; col++)
  {
    Console.Write("matrix[{0},{1}] = ",row, col);
    matrix[row, col] = int.Parse(Console.ReadLine());
  }
}

for (int row = 0; row < matrix.GetLength(0); row++)
{
  for (int col = 0; col < matrix.GetLength(1); col++)
  {
    Console.Write(" " + matrix[row, col]);
  }
  Console.WriteLine();
}

//  Enter the number of the rows: 3
//  Enter the number of the columns: 2
//  Enter the cells of the matrix:
//  matrix[0,0] = 2
//  matrix[0,1] = 3
//  matrix[1,0] = 5
//  matrix[1,1] = 10
//  matrix[2,0] = 8
//  matrix[2,1] = 9
//  2 3
//  5 10
//  8 9