Используя стандартные библиотеки, как предложено в моем комментарии, вы можете реализовать это примерно так:
#include <iostream>
#include <vector>
#include <iterator>
#include <random>
#include <algorithm>
int main()
{
int n, m;
std::cout << "n = ";
std::cin >> n;
std::cout << "m = ";
std::cin >> m;
// Creates a matrix of N x M elements
std::vector<std::vector<int>> array(n, std::vector<int>(m));
// Create a random-number generator for the range 1 to 20 (inclusive)
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 20);
// Generate random numbers filling our matrix
for (auto& v : array)
{
std::generate(begin(v), end(v), [&]() { return dis(gen); });
}
// Print the matrix before swapping
for (auto const& v : array)
{
std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
// Find the min/max of the first row in the matrix
auto minmax_pair = std::minmax_element(begin(array[0]), end(array[0]));
// From the pair of min/max iterators fetched above, get the indexes
auto min_index = std::distance(begin(array[0]), minmax_pair.first);
auto max_index = std::distance(begin(array[0]), minmax_pair.second);
// Now swap the columns of the min and max
for (auto& v : array)
{
std::swap(v[min_index], v[max_index]);
}
// Print the matrix after swapping
std::cout << '\n';
for (auto const& v : array)
{
std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
}