Advertisement
cska1312

03. Symbol in Matrix

May 15th, 2023
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. vector<std::vector<char>> readInput(const int& matrixSize)
  5. {
  6.  
  7.     vector<std::vector<char>> matrix {};
  8.  
  9.     char symbol = '_';
  10.  
  11.     for (int row = 0; row < matrixSize; ++row)
  12. {
  13.         vector<char> rowSymbols {};
  14.         for (int col = 0; col < matrixSize; ++col)
  15. {
  16.             cin >> symbol;
  17.             rowSymbols.push_back(symbol);
  18.         }
  19.  
  20.         matrix.push_back(rowSymbols);
  21.     }
  22.  
  23.     return matrix;
  24. }
  25.  
  26. bool findSearchedElement(const std::vector<std::vector<char>>& matrix, const char& findEelement, int& rowIndex, int& colIndex)
  27. {
  28.  
  29.     bool isFound = false;
  30.  
  31.     for (int i = 0; i < matrix.size(); ++i)
  32.  {
  33.         for (int j = 0; j < matrix[i].size(); ++j)
  34. {
  35.             if(matrix[i][j] == findEelement)
  36. {
  37.                 isFound = true;
  38.                 rowIndex = i;
  39.                 colIndex = j;
  40.                 break;
  41.             }
  42.         }
  43.  
  44.         if(isFound){
  45.             break;
  46.         }
  47.     }
  48.  
  49.     return isFound;
  50. }
  51.  
  52. void printResult(const bool& isFound, const char& searchedElement, const int& rowIndex, const int& colIndex)
  53. {
  54.     if(isFound)
  55. {
  56.         cout << "(" << rowIndex << ", " << colIndex << ")" << endl;
  57.     }
  58.     else {
  59.         cout << searchedElement << " does not occur in the matrix " << endl;
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.  
  66.     int matrixSize = 0;
  67.     cin >> matrixSize;
  68.  
  69.     vector<std::vector<char>> matrix = readInput(matrixSize);
  70.  
  71.     char searchedElement = '_';
  72.     cin >> searchedElement;
  73.  
  74.     int rowIndex = 0;
  75.     int colIndex = 0;
  76.  
  77.     bool isFound = findSearchedElement(matrix, searchedElement, rowIndex, colIndex);
  78.  
  79.     printResult(isFound, searchedElement, rowIndex, colIndex);
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement