# Sort an Array in C++: Step by Step Solution

## Sort the Array Using Pointers

Problem

In the 7×7 array of int type in each column, we should order the elements located between the maximum and minimum.

First, we need to find the minimum and maximum elements of the array. Then sort in ascending or descending order all the elements which are between the maximum and minimum elements. There are situations where the maximum and minimum elements are close together, and the array will remain unchanged.
Note: we should solve this problem using pointers.

Solution
To solve this problem we will use C++, and in particular, the pointers. We will compare not the values of the matrix elements, but their addresses. Here is the source code:

```
#include
#include
#include
#include

using namespace std;

int main()
{
int matrix[7][7]; // starting matrix

srand (time(0)); // random fulfilment

// input/output matrix
cout &lt;&lt; "Starting matrix: \n";
for (int i=0; i&lt;7; i++) {
for (int j=0; j&lt;7; j++) {
matrix[i][j] = rand() % 10; // random fulfilment
cout &lt;&lt; setw(3) &lt;&lt; matrix[i][j];
}
cout &lt;&lt; endl;
}

int sorting;

int *max, // pointer for the maximal value
*min; // pointer for the minimal value

for (int j=0; j&lt;7; j++)
{
max = &amp;matrix[0][j];
min = &amp;matrix[0][j];

// find the max and min elements addresses
for (int i=1; i&lt;7; i++)
{
if (matrix[i][j] &lt; *min) min = &amp;matrix[i][j]; // remember the max value address if (matrix[i][j] &gt; *max)
max = &amp;matrix[i][j]; // remember the min value address
}

// sorting
for (int i=0; i&lt;7; i++) { if (max &gt; min) // if max value is before the min
for (int k=1; k&lt;7; k++) if (matrix[k-1][j] &gt; matrix[k][j]) // ascending sorting
{
sorting = matrix[k-1][j]; // remember the previous value
matrix[k-1][j] = matrix[k][j]; // change it to the current
matrix[k][j] = sorting; // change the current value to the previous
}

else if (max &lt; min) // if min value is before the max
for (int k=1; k&lt;7; k++)
if (matrix[k-1][j] &lt; matrix[k][j]) // descending sort
{
sorting = matrix[k-1][j]; // remember the previous value
matrix[k-1][j] = matrix[k][j]; // change it to the current
matrix[k][j] = sorting; // change the current value to the previous
}

}

}

cout &lt;&lt; "\nChanged matrix\n";

for (int i=0; i&lt;7; i++)
{
for (int j=0; j&lt;7; j++)
cout &lt;&lt; setw(3) &lt;&lt; matrix[i][j];
cout &lt;&lt; endl;
}

return 0;
}
```

The program works properly. Here is the output:
Starting matrix:
1 3 6 1 3 1 2
4 2 9 9 1 3 3
2 0 0 6 3 5 7
7 3 9 9 3 1 7
0 6 1 4 1 9 7
4 0 0 0 2 1 1
5 4 5 8 4 7 4

Changed matrix:
1 0 6 1 1 1 2
4 0 9 9 1 1 3
2 2 0 6 2 1 7
7 3 9 9 3 3 7
0 3 1 4 3 5 7
4 4 0 0 3 7 1
5 6 5 8 4 9 4
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}