skipter icon

Symbol In Matrix

skipter | PRO | 09/23/18 05:39:11 PM UTC | 0 ⭐ | 12231 👁️ | Never ⏰ | []
C# |

1.91 KB

|

None

|

0 👍

/

0 👎

using System;
 
namespace _04.SymbolInMatrix
{
    class SymbolInMatrix
    {
        static void Main()
        {   //Read matrix size.
            int matrixSize = Int32.Parse(Console.ReadLine());
            string[,] matrix = new string[matrixSize, matrixSize];
 
            //Initial matrix.
            for (int row = 0; row < matrix.GetLength(0); row++)
            {  
                //Read columns.
                string columnElements = Console.ReadLine();
                char[] symbols = new char[columnElements.Length];
                for (int i = 0; i < columnElements.Length; i++)
                {
                    symbols[i] = columnElements[i];
                }
 
                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    matrix[row, col] = symbols[col].ToString();
                }
            }
            //Input symbol.
            string symbol = Console.ReadLine();
 
            int rowIndex = 0;
            int colIndex = 0;
            bool isContainsSymbol = false;
 
            #region CheckForSymbol
            //Check for symbol.
            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (matrix[row, col] == symbol)
                    {
                        isContainsSymbol = true;
                        rowIndex = row;
                        colIndex = col;
                        break;
                    }
                }
            }
            #endregion
 
            //Print result.
            if (isContainsSymbol == true)
            {
                Console.WriteLine($"({rowIndex}, {colIndex})");
            }
            else
            {
                Console.WriteLine($"{symbol} does not occur in the matrix");
            }
        }
    }
}

Comments