Advertisement
titytus

Sort_Matrix_2a

Oct 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. void Xuat(int A[][4], int row, int col);
  3. void Sort(int A[][4], int row, int col);
  4. void swap(int A[][4], int iR, int iC, int jR, int jC);
  5.  
  6. int main()
  7. {
  8.     int A[3][4]=
  9.     {
  10.         {7,4,0,4},
  11.         {3,8,1,5},
  12.         {9,2,6,3}
  13.     };
  14.     int row=3;
  15.     int col=4;
  16.     //Xuat(A,row,col);
  17.     Sort(A,row,col);
  18.     Xuat(A,row,col);
  19.     return 0;
  20. }
  21.  
  22. void Xuat(int A[][4], int row, int col)
  23. {
  24.     for (int i=0;i<row;i++)
  25.     {
  26.         for (int j=0;j<col;j++)
  27.             printf("%d ",A[i][j]);
  28.         printf("\n");
  29.     }
  30.        
  31. }
  32.  
  33. void Sort(int A[][4], int row, int col)
  34. {
  35.     for (int i=0;i<row*col-1;i++)
  36.         for (int j=i+1;j<row*col;j++)
  37.         {
  38.             if (A[i/col][i%col]>A[j/col][j%col])
  39.                 swap(A,i/col,i%col,j/col,j%col);
  40.         }
  41. }
  42.  
  43. void swap(int A[][4], int iR, int iC, int jR, int jC)
  44. {
  45.     int tmp=A[iR][iC];
  46.     A[iR][iC]=A[jR][jC];
  47.     A[jR][jC]=tmp;
  48. }
  49.  
  50. /*
  51. 00 01 02 03 04 05 06 07 08 09
  52. 10 11 12 13 14 15 16 17 18 19
  53. 20 21 22 23 24 25 26 27 28 29
  54.  
  55. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement