Venciity

[ Telerik C#] MultidimArrays-Demos 1. ReadWriteMatrix

Feb 26th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System;
  2.  
  3. class ReadWriteMatrix
  4. {
  5.     static void Main()
  6.     {
  7.         // Read the matrix dimensions
  8.         Console.Write("Number of rows = ");
  9.         int rows = int.Parse(Console.ReadLine());
  10.         Console.Write("Number of columns = ");
  11.         int cols = int.Parse(Console.ReadLine());
  12.  
  13.         // Allocate the matrix
  14.         int[,] matrix = new int[rows, cols];
  15.  
  16.         // Enter the matrix elements
  17.         for (int row = 0; row < rows; row++)
  18.         {
  19.             for (int col = 0; col < cols; col++)
  20.             {
  21.                 Console.Write("matrix[{0},{1}] = ", row, col);
  22.                 int element = int.Parse(Console.ReadLine());
  23.                 matrix[row, col] = element;
  24.             }
  25.         }
  26.  
  27.         // Print the matrix on the console
  28.         Console.WriteLine();
  29.         Console.WriteLine("The matrix is as follows:");
  30.         for (int row = 0; row < rows; row++)
  31.         {
  32.             for (int col = 0; col < cols; col++)
  33.             {
  34.                 Console.Write("{0} ", matrix[row, col]);
  35.             }
  36.             Console.WriteLine();
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment