Your function is declared as
void swap_min(int *m[], unsigned rows, unsigned cols);
Its first parameter is given as int *m[] , that is, it is an array of indefinite length, whose elements are of type int * , that is, an array of pointers. From the condition of the problem it is obvious that this is a dynamically allocated one-dimensional array of pointers to one-dimensional arrays.
Implicitly, this parameter is converted to the int **m type, and is a pointer to the first element of the array, if an array was indeed passed as an argument. But in view of this type parameter conversion, you can also pass an int ** type pointer as an argument.
This array could be defined as follows:
int **m; m = new int *[rows]; for ( unsigned int i = 0; i < rows; i++ ) { m[i] = new int [cols]; } // заполнение массива значениями. swap_min( m, rows, cols );
Below is a demonstration program.
#include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> void swap_min( int *m[], unsigned int rows, unsigned int cols ) { unsigned int min_row = 0, min_col = 0; for ( unsigned int i = 0; i != rows ; i++ ) { for ( unsigned int j = 0; j != cols ; j++ ) { if ( m[i][j] < m[min_row][min_col] ) { min_row = i; min_col = j; } } } if ( min_row != 0 ) { int *tmp = m[0]; m[0] = m[min_row]; m[min_row] = tmp; } } int main() { unsigned int rows = 4; unsigned int cols = 5; int **m = new int *[rows]; for ( unsigned int i = 0; i < rows; i++ ) { m[i] = new int[cols]; } std::srand( ( unsigned int )std::time( nullptr ) ); for ( unsigned int i = 0; i < rows; i++ ) { for ( unsigned int j = 0; j < cols; j++ ) { m[i][j] = std::rand() % ( rows * cols ); } } for ( unsigned int i = 0; i < rows; i++ ) { for ( unsigned int j = 0; j < cols; j++ ) { std::cout << std::setw( 2 ) << m[i][j] << ' '; } std::cout << std::endl; } std::cout << std::endl; swap_min( m, rows, cols ); for ( unsigned int i = 0; i < rows; i++ ) { for ( unsigned int j = 0; j < cols; j++ ) { std::cout << std::setw( 2 ) << m[i][j] << ' '; } std::cout << std::endl; } std::cout << std::endl; for ( unsigned int i = 0; i < rows; i++ ) { delete [] m[i]; } delete [] m; }
Its output to the console might look like this:
7 1 11 17 13 14 0 8 3 2 3 14 1 9 9 1 12 16 18 12 14 0 8 3 2 7 1 11 17 13 3 14 1 9 9 1 12 16 18 12
You could also use the standard std::min_element to search for the minimum element in a string and std::swap to exchange the values of two elements.
Keep in mind that in fact, a one-dimensional array of arrays is taken to the input of a function, and not a two-dimensional array, as you say in the description.
To work with two-dimensional arrays, the program may look as follows
#include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> const unsigned int rows = 4; const unsigned int cols = 5; void swap_min( int m[][cols], unsigned int rows ) { int tmp[cols]; unsigned int min_row = 0, min_col = 0; for ( unsigned int i = 0; i != rows ; i++ ) { for ( unsigned int j = 0; j != cols ; j++ ) { if ( m[i][j] < m[min_row][min_col] ) { min_row = i; min_col = j; } } } // std::swap( m[0], m[min_row] ); if ( min_row != 0 ) { for ( unsigned int i = 0; i < cols; i++ ) tmp[i] = m[0][i]; for ( unsigned int i = 0; i < cols; i++ ) m[0][i] = m[min_row][i]; for ( unsigned int i = 0; i < cols; i++ ) m[min_row][i] = tmp[i]; } } int main() { int m[rows][cols]; std::srand( ( unsigned int )std::time( nullptr ) ); for ( unsigned int i = 0; i < rows; i++ ) { for ( unsigned int j = 0; j < cols; j++ ) { m[i][j] = std::rand() % ( rows * cols ); } } for ( unsigned int i = 0; i < rows; i++ ) { for ( unsigned int j = 0; j < cols; j++ ) { std::cout << std::setw( 2 ) << m[i][j] << ' '; } std::cout << std::endl; } std::cout << std::endl; swap_min( m, rows ); for ( unsigned int i = 0; i < rows; i++ ) { for ( unsigned int j = 0; j < cols; j++ ) { std::cout << std::setw( 2 ) << m[i][j] << ' '; } std::cout << std::endl; } std::cout << std::endl; }
Its approximate console output
12 10 15 14 16 7 18 1 16 14 9 6 1 9 18 4 8 15 11 3 7 18 1 16 14 12 10 15 14 16 9 6 1 9 18 4 8 15 11 3
Again, for the exchange of two rows of a two-dimensional array, you could use the standard std::swap algorithm, as indicated in the comments inside the function.