## 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 << "Starting matrix: \n"; for (int i=0; i<7; i++) { for (int j=0; j<7; j++) { matrix[i][j] = rand() % 10; // random fulfilment cout << setw(3) << matrix[i][j]; } cout << endl; } int sorting; int *max, // pointer for the maximal value *min; // pointer for the minimal value for (int j=0; j<7; j++) { max = &matrix[0][j]; min = &matrix[0][j]; // find the max and min elements addresses for (int i=1; i<7; i++) { if (matrix[i][j] < *min) min = &matrix[i][j]; // remember the max value address if (matrix[i][j] > *max) max = &matrix[i][j]; // remember the min value address } // sorting for (int i=0; i<7; i++) { if (max > min) // if max value is before the min for (int k=1; k<7; k++) if (matrix[k-1][j] > 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 < min) // if min value is before the max for (int k=1; k<7; k++) if (matrix[k-1][j] < 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 << "\nChanged matrix\n"; for (int i=0; i<7; i++) { for (int j=0; j<7; j++) cout << setw(3) << matrix[i][j]; cout << 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

Thanks for your attention!

There are often problems with choosing the way to sort arrays in C++ compiler packages. Basically all the methods have only one goal: compare the elements and change their positions if they are in the wrong place. One of our experts has completed a task to sort an array with the elements in ascending or descending order using C++ and pointers. You can use this information to expand your knowledge to complete your computer science assignment.

If you are experiencing difficulties with the implementation of your computer science assignment, there is no reason to be sad! You have a chance to ask real experts for help. If you are a beginner that has received a difficult assignment, Assignment.Essayshark will provide assistance for any assignment of any complexity. All of our papers are original and created for clientsâ€™ specific orders. During the ordering process, you need to attach all documents and information that should be considered by our experts. With the help of our completed assignments you can increase your knowledge in a particular discipline and improve your ratings.

Our service is available 24/7 for providing expert help with all types of homework in computer science and other disciplines. Our experts are punctual with the delivery of completed orders; you can be confident that you will submit your homework before the deadline.