Advertisement
SergeyPGUTI

8.2.11

Feb 19th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string.h>
  4. #include<cstdlib>
  5. #include<ctime>
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11. int main()
  12. {
  13.     int n,m,temp,lastJ,row=0,colomn=0,sum;
  14.     double tempDouble;
  15.     srand((unsigned)time(0));
  16.     cin>>n;
  17.     cin>>m;
  18.     int **p= new int*[n];
  19.  
  20.     for (int i=0;i<n;i++)
  21.     {
  22.         p[i]=new int[m];
  23.     }
  24.  
  25.     for (int i=0;i<n;i++)
  26.         for (int j=0;j<m;j++)
  27.             p[i][j]=rand()%2;
  28.  
  29.  
  30.      for (int i=0;i<n;i++)
  31.     {
  32.  
  33.         for (int j=0;j<m;j++)
  34.             {
  35.                 cout.width(3);
  36.                 cout<<p[i][j]<<" ";
  37.             }
  38.             cout<<endl;
  39.     }
  40. cout<<endl;cout<<endl;cout<<endl;
  41.  
  42.     // SORT
  43.  
  44.     double **a= new double*[2];  //массив со значениями ср ар столбцов
  45.  
  46.     for (int i=0;i<2;i++)
  47.     {
  48.         a[i]=new double[m];
  49.     }
  50.  
  51.         for (int j=0;j<m;j++) //номер столбца
  52.             a[0][j]=j;
  53.         for (int j=0;j<m;j++)//среднее арфифметическое столбцов
  54.             a[1][j]=0;
  55.  
  56.     for(int j=0;j<m;j++) // заполним массим а значениями ср ар
  57.     {
  58.         for (int i=0;i<n;i++)
  59.         {
  60.             a[1][j]+=p[i][j];
  61.         }
  62.         a[1][j]/=n;
  63.     }
  64.  
  65.  
  66.      for(int i=0;i<m;i++) // сортировка массива а
  67.     {
  68.         for (int j=0;j<n-i-1;j++)
  69.         {
  70.             if (a[1][j]>a[1][j+1])
  71.             {
  72.                 tempDouble=a[1][j];
  73.                 a[1][j]=a[1][j+1];
  74.                 a[1][j+1]=tempDouble;
  75.  
  76.                 temp=a[0][j];
  77.                 a[0][j]=a[0][j+1];
  78.                 a[0][j+1]=temp;
  79.             }
  80.  
  81.         }
  82.     }
  83.  
  84.     int **p2= new int*[n];//новый массив куда запишем отсортированный
  85.  
  86.     for (int i=0;i<n;i++)
  87.     {
  88.         p2[i]=new int[m];
  89.     }
  90.  
  91.  
  92.     for (int j=0;j<m;j++)// записываем в новвый массив старый с учетом сортировки
  93.     {
  94.              for (int i=0;i<n;i++)
  95.         {
  96.             p2[i][j]=p[i][(int)a[0][j]];
  97.         }
  98.     }
  99.  
  100.     // END OF SORT
  101.  
  102.  
  103.     for (int i=0;i<n;i++)
  104.     {
  105.  
  106.         for (int j=0;j<m;j++)
  107.             {
  108.                 cout.width(3);
  109.                 cout<<p2[i][j]<<" ";
  110.             }
  111.             cout<<endl;
  112.     }
  113.  
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement