Advertisement
boyski33

Print a matrix on the console

Dec 29th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class PrintMatrix
  6. {
  7.     static void Main()
  8.     {
  9.         string[] separators = { " ", "\t" };
  10.  
  11.         Console.WriteLine("Enter N (width/height of the matrix): ");
  12.         int n = int.Parse(Console.ReadLine());
  13.  
  14.         int[,] matrix = new int[n, n];
  15.  
  16.         for (int row = 0; row < matrix.GetLength(0); row++)
  17.         {
  18.             Console.WriteLine("Enter all {0} values on row number {1} separated by space:", n, (row + 1));
  19.             string currentLine = Console.ReadLine();
  20.             string[] numbersOnLine = currentLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  21.  
  22.             for (int col = 0; col < matrix.GetLength(1); col++)
  23.             {
  24.                 if (col < numbersOnLine.Length)
  25.                 {
  26.                     matrix[row, col] = int.Parse(numbersOnLine[col]);
  27.                 }
  28.             }
  29.         }
  30.  
  31.         for (int row = 0; row < matrix.GetLength(0); row++)
  32.         {
  33.             for (int col = 0; col < matrix.GetLength(1); col++)
  34.             {
  35.                 Console.Write("{0}".PadRight(15 - matrix[row,col].ToString().Length), matrix[row, col]);
  36.             }
  37.  
  38.             Console.WriteLine();
  39.         }
  40.  
  41.  
  42.  
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement