Advertisement
Sanlover

Untitled

Dec 18th, 2021 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <Windows.h>
  4. using namespace std;
  5.  
  6. vector<pair<size_t, size_t>> getMaxIndexes(int** matrix, const size_t& rows, const size_t& cols)
  7. {
  8. vector<pair<size_t, size_t>> indexes;
  9.  
  10. size_t maxRow = 0, maxCol = 0;
  11. // loop, that finds the max indexes, where elements have the max value
  12. for (size_t i = 0; i < rows; i++)
  13. {
  14. for (size_t j = 0; j < cols; j++)
  15. {
  16. if (matrix[i][j] > matrix[maxRow][maxCol])
  17. {
  18. maxCol = j;
  19. maxRow = i;
  20. }
  21. }
  22. }
  23. // loop that adds indexes, where elements have the max value from all the matrix to the result vector
  24. for (size_t i = 0; i < rows; i++)
  25. {
  26. for (size_t j = 0; j < cols; j++)
  27. {
  28. if (matrix[i][j] == matrix[maxRow][maxCol])
  29. {
  30. indexes.push_back(pair<size_t, size_t>(i, j));
  31. }
  32. }
  33. }
  34. return indexes;
  35. }
  36.  
  37. int main()
  38. {
  39. SetConsoleOutputCP(1251);
  40. int** matrix;
  41. int rows, cols;
  42. cout << "Введите размеры матрицы" << endl;
  43. cout << "Ряды (i) = ";
  44. cin >> rows;
  45. cout << "Колонки (j) = ";
  46. cin >> cols;
  47.  
  48. matrix = new int*[rows];
  49. for (size_t i = 0; i < rows; i++)
  50. {
  51. matrix[i] = new int[cols];
  52. }
  53. cout << endl << "Заполните массив:" << endl;
  54.  
  55. for (size_t i = 0; i < rows; i++)
  56. {
  57. for (size_t j = 0; j < cols; j++)
  58. {
  59. cout << "[" << i << "][" << j << "] = ";
  60. cin >> matrix[i][j];
  61. }
  62. }
  63.  
  64. vector<pair<size_t, size_t>> maxIndexes = getMaxIndexes(matrix, rows, cols);
  65. cout << endl << "Список индексов, где элемент имеет максимальное значение среди всей матрицы" << endl;
  66. for (size_t i = 0; i < maxIndexes.size(); i++)
  67. {
  68. cout << i << ") " << maxIndexes[i].first << ":" << maxIndexes[i].second << endl;
  69. }
  70. return 0;
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement