You are already using a standard function like std::swap . So why don't you use standard algorithms?
The program might look like this.
#include <iostream> #include <iomanip> #include <algorithm> #include <iterator> #include <cstdlib> #include <ctime> int main() { const size_t ROW = 3; const size_t COLUMN = 3; int arr[ROW][COLUMN]; std::srand((unsigned int)std::time(nullptr)); for (auto &row : arr) { std::generate(std::begin(row), std::end(row), [] { return std::rand() % 100 - 50; }); } for (const auto &row : arr) { for (auto x : row) std::cout << std::setw(3) << std::right << x << ' '; std::cout << std::endl; } std::cout << std::endl; for (size_t i = 0; i < ROW; i++) { int *max = std::max_element(std::begin(arr[i]), std::end(arr[i])); std::iter_swap(max, arr[i] + i); } for (const auto &row : arr) { for (auto x : row) std::cout << std::setw(3) << std::right << x << ' '; std::cout << std::endl; } }
The output of the program to the console, for example, can be
27 8 41 41 -41 -4 -7 -28 -35 41 8 27 -41 41 -4 -35 -28 -7
If you want to find the maximum element in each row of the matrix yourself, you can use the following cycles
for (size_t i = 0; i < ROW; i++) { size_t max = 0; for (size_t j = 1; j < COLUMN; j++) { if (arr[i][max] < arr[i][j]) max = j; } std::swap(arr[i][max], arr[i][i]); }
As for your cycles to find the maximum element and exchange with the element of the diagonal
for (int i = 0; i < column; i++) { int max = arr[i][i]; for (int j = 0; j < row; j++) if(arr[i][j] > max){ max = arr[i][j]; } swap(max, arr[i][i]); }
then, first, you confused the columns with rows, although for a square matrix this is not important, but nevertheless, exchange the value of the local variable max , declared as
int max = arr[i][i];
with a diagonal element