Sorting Two Dimensional Array in C++

  1. #include<iostream>  
  2. #include<cstdlib>  
  3.   
  4. #define ROW 4  
  5. #define COL 2  
  6.   
  7. using namespace std;  
  8.   
  9. void sort(int [][COL]);  
  10.   
  11. int main()  
  12. {  
  13.    int num[ROW][COL];  
  14.    int row,col;  
  15.      
  16.    for(row=0;row<ROW;row++)  
  17.    {  
  18.     for(col=0;col<COL;col++)  
  19.     {  
  20.      cout<<"Enter number at Row "<<row<<" Coloumn " <<col<<" : ";  
  21.      cin>>num[row][col];  
  22.     }  
  23.    }  
  24.       
  25.    sort(num);  
  26.      
  27.    cout<<"\n* list in Assending Order is :\nID     Marks\n";  
  28.      
  29.    for(row=0;row<ROW;row++)  
  30.    {  
  31.     for(col=0;col<COL;col++)  
  32.     {  
  33.      cout<<num[row][col]<<"\t";  
  34.     }  
  35.     cout<<endl;  
  36.    }  
  37.   
  38.    system("pause");  
  39.    return 0;  
  40.   
  41. }  
  42.   
  43.   
  44. void sort(int list[][COL])  
  45. {  
  46.      int out,in,temp,temp2;  
  47.        
  48.        for(out=0;out<ROW-1;out++)  
  49.        {  
  50.         for(in=out+1;in<ROW;in++)  
  51.         {  
  52.          if(list[in][0]<list[out][0])  
  53.          {   
  54.           temp=list[out][0];  
  55.           temp2=list[out][1];  
  56.   
  57.           list[out][0]=list[in][0];  
  58.           list[out][1]=list[in][1];  
  59.   
  60.           list[in][0]=temp;  
  61.           list[in][1]=temp2;  
  62.          }                        
  63.         }  
  64.        }  
  65. }