Advertisement
skipter

Symbol In Matrix

Sep 23rd, 2018
12,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _04.SymbolInMatrix
  4. {
  5.     class SymbolInMatrix
  6.     {
  7.         static void Main()
  8.         {   //Read matrix size.
  9.             int matrixSize = Int32.Parse(Console.ReadLine());
  10.             string[,] matrix = new string[matrixSize, matrixSize];
  11.  
  12.             //Initial matrix.
  13.             for (int row = 0; row < matrix.GetLength(0); row++)
  14.             {  
  15.                 //Read columns.
  16.                 string columnElements = Console.ReadLine();
  17.                 char[] symbols = new char[columnElements.Length];
  18.                 for (int i = 0; i < columnElements.Length; i++)
  19.                 {
  20.                     symbols[i] = columnElements[i];
  21.                 }
  22.  
  23.                 for (int col = 0; col < matrix.GetLength(1); col++)
  24.                 {
  25.                     matrix[row, col] = symbols[col].ToString();
  26.                 }
  27.             }
  28.             //Input symbol.
  29.             string symbol = Console.ReadLine();
  30.  
  31.             int rowIndex = 0;
  32.             int colIndex = 0;
  33.             bool isContainsSymbol = false;
  34.  
  35.             #region CheckForSymbol
  36.             //Check for symbol.
  37.             for (int row = 0; row < matrix.GetLength(0); row++)
  38.             {
  39.                 for (int col = 0; col < matrix.GetLength(1); col++)
  40.                 {
  41.                     if (matrix[row, col] == symbol)
  42.                     {
  43.                         isContainsSymbol = true;
  44.                         rowIndex = row;
  45.                         colIndex = col;
  46.                         break;
  47.                     }
  48.                 }
  49.             }
  50.             #endregion
  51.  
  52.             //Print result.
  53.             if (isContainsSymbol == true)
  54.             {
  55.                 Console.WriteLine($"({rowIndex}, {colIndex})");
  56.             }
  57.             else
  58.             {
  59.                 Console.WriteLine($"{symbol} does not occur in the matrix");
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement