kolioi

32. Find INDEX in dual array C++

Dec 9th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main()
  5. {
  6.     using namespace std;
  7.  
  8.     int num_to_find;
  9.     cin >> num_to_find;
  10.  
  11.     int row_number, col_number;
  12.     cin >> row_number >> col_number;
  13.  
  14.     vector<vector<int>> matrix;
  15.     for (int row = 0; row < row_number; ++row)
  16.     {
  17.         vector<int> matrix_row;
  18.         for (int col = 0; col < col_number; ++col)
  19.         {
  20.             int n;
  21.             cin >> n;
  22.             matrix_row.push_back(n);
  23.         }
  24.         matrix.push_back(matrix_row);
  25.     }
  26.  
  27.     bool found = false;
  28.     for (int row = 0; row < row_number; ++row)
  29.         for (int col = 0; col < col_number; ++col)
  30.             if (matrix[row][col] == num_to_find)
  31.             {
  32.                 cout << row << col << endl;
  33.                 found = true;
  34.             }
  35.  
  36.     if (!found)
  37.         cout << "Number not found\n";
  38.  
  39.     return 0;
  40. }
  41.  
  42. //#include <iostream>
  43. //#include <vector>
  44. //int main()
  45. //{
  46. //  using namespace std;
  47. //
  48. //  int num_to_find;
  49. //  cin >> num_to_find;
  50. //
  51. //  int rows, cols;
  52. //  cin >> rows >> cols;
  53. //
  54. //  vector<int> matrix;
  55. //  for (int i = 0; i < rows * cols; ++i)
  56. //  {
  57. //      int n;
  58. //      cin >> n;
  59. //      matrix.push_back(n);
  60. //  }
  61. //
  62. //  bool found = false;
  63. //  for (int i = 0; i < matrix.size(); ++i)
  64. //      if (matrix[i] == num_to_find)
  65. //      {
  66. //          int row = i / cols, col = i % cols;
  67. //          cout << row << col << endl;
  68. //          found = true;
  69. //      }
  70. //
  71. //  if (!found)
  72. //      cout << "Number not found\n";
  73. //
  74. //  return 0;
  75. //}
Advertisement
Add Comment
Please, Sign In to add comment