Advertisement
SergeyPGUTI

8.2.13

Feb 24th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 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. class MyArray{
  10.     private:
  11.     int* Arr;
  12.     int length;
  13.     int n;
  14.     int m;
  15.  
  16.     public:
  17.     MyArray(int n,int m)
  18.     {
  19.         length=n*m;
  20.         Arr=new int[length];
  21.         this->n=n;
  22.         this->m=m;
  23.         for(int i=0;i<length;i++)
  24.         {
  25.             Arr[i]=i;
  26.         }
  27.     }
  28.  
  29.     void ChangeElement(int x,int y,int a)
  30.     {
  31.         int id=x*m;
  32.         id+=y;
  33.         Arr[id]=a;
  34.     }
  35.  
  36.     void Output(){
  37.         cout<<endl;
  38.         for(int i=0,j=0;i<length;i++,j++)
  39.         {
  40.             if (j==m) {cout<<endl;j=0;}
  41.             cout.width(3);
  42.             cout<<Arr[i]<<" ";
  43.  
  44.         }
  45.     }
  46.  
  47.     void lineSum()
  48.     {
  49.         int Sum=0;
  50.         int lineNumber=0;
  51.         cout<<endl;
  52.         for(int i=0,j=0;i<length;i++,j++)
  53.         {
  54.             Sum+=Arr[i];
  55.             if(j==m-1) {
  56.                 cout<<"Summ in line "<<lineNumber<<" - "<<Sum<<endl;
  57.                 lineNumber++;
  58.                 Sum=0;
  59.                 j=-1;
  60.             }
  61.         }
  62.         cout<<endl;
  63.     }
  64.  
  65.     void columnSum()
  66.     {
  67.         cout<<endl;
  68.         int *Sum=new int[m];
  69.         for(int i=0;i<m;i++)
  70.         {
  71.             Sum[i]=0;
  72.         }
  73.         for(int i=0,j=0;i<length;i++,j++)
  74.         {
  75.             if (j==m) j=0;
  76.             Sum[j]+=Arr[i];
  77.         }
  78.         for(int i=0;i<m;i++)
  79.         {
  80.             cout<<"Summ in column "<<i<<" - "<<Sum[i]<<endl;
  81.         }
  82.         cout<<endl;
  83.     }
  84.  
  85. };;
  86.  
  87.  
  88. int main()
  89. {
  90.     int n=3;
  91.     int m=4;
  92.     MyArray a(n,m);
  93.     srand((unsigned)time(0));
  94.  
  95.     for (int i=0;i<n;i++)
  96.         for (int j=0;j<m;j++)
  97.            a.ChangeElement(i,j,rand()%10);
  98.  
  99.     a.ChangeElement(2,2,0);
  100.     a.Output();
  101.     a.lineSum();
  102.     a.columnSum();
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement