C++ program to sort elements in a matrix

This C++ program sorts the elements of a 2D matrix row by row using pointer. The row and column values of the matrix are read into the variables m and n. Then the program uses nested for loop to read the elements to the matrix a[][] using pointers, also each elements are stored into the pointer array p[]. The elements in the pointer array is sorted and the new array is written to the original 2D array a[][] overwriting the existing elements. Then the sorted 2D array is displayed to the output screen.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
          int a[20][20],i,j,m,n,*p[10],temp,b[20],s,*p1;
          p1=b;
          clrscr();
          cout<<"\n\nEnter the order of the matrix: ";
          cin>>m>>n;
          s=0;
          cout<<"\nEnter the elements:";
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             cin>>*(*(a+i)+j);
                             p[i]=(*(a+i)+j);
                             p1[s]=*(*(a+i)+j);
                             s++;
                   }
          }
          for(i=0;i<s;i++)
          {
                   for(j=i+1;j<s;j++)
                   {
                             if(p1[i]>p1[j])
                             {
                                      temp=p1[i];
                                      p1[i]=p1[j];
                                      p1[j]=temp;
                             }
                   }
          }
          s=0;
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             *(*(a+i)+j)=p1[s];
                             s++;
                   }
          }
          cout<<"\nMatrics after sorting:";
          for(i=0;i<m;i++)
          {
                   cout<<"\n\n";
                   for(j=0;j<n;j++)
                   {
                             cout<<*(*(a+i)+j)<<"\t";
                   }
          }
          getch();
}