You are given an N × M matrix. By rearranging its rows and columns, move the largest element
You are given an N × M matrix. By rearranging its rows and columns, move the largest element to the upper left corner. Determine if the smallest element can be placed in the lower right corner in the same way.
int main () {
// Something
int a [n] [m];
// Init
int im = 0, jm = 0, max = a [0] [0]; // in this interval, find the maximum through the cycle;
for (int i = 0; i <n; ++ i)
for (int j = 0; j <m; ++ j)
if (a [i] [j]> max) {
max = a [i] [j];
im = i;
jm = j;
}
swapCollumn (a, n, m, im, n-1); // Using the formula, put the maximum down / in place;
swapRow (a, n, m, jm, m-1);
}
void swapCollumn (int ** a, int n, int m, int c1, int c2) // Put the columns in their places;
{
if (c1 <0 || c2 <0 || c1> = n || c2> = n)
return;
for (int i = 0; i <m; ++ i)
swap (& a [c1] [i], & a [c2] [i]);
}
void swapRow (int ** a, int n, int m, int r1, int r2) // Move rows in places;
{
if (r1 <0 || r2 <0 || r1> = m || r2> = m)
return;
for (int i = 0; i <n; ++ i)
swap (& a [i] [r1], & a [i] [r2]);
}
void swap (int * el1, int el2) // Display to the user on the screen;
{
int t = * el1;
* el1 = * el2;
* el2 = * el1;
}