Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string.h>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- class MyArray{
- private:
- int* Arr;
- int length;
- int n;
- int m;
- public:
- MyArray(int n,int m)
- {
- length=n*m;
- Arr=new int[length];
- this->n=n;
- this->m=m;
- for(int i=0;i<length;i++)
- {
- Arr[i]=i;
- }
- }
- void ChangeElement(int x,int y,int a)
- {
- int id=x*m;
- id+=y;
- Arr[id]=a;
- }
- void Output(){
- cout<<endl;
- for(int i=0,j=0;i<length;i++,j++)
- {
- if (j==m) {cout<<endl;j=0;}
- cout.width(3);
- cout<<Arr[i]<<" ";
- }
- }
- void lineSum()
- {
- int Sum=0;
- int lineNumber=0;
- cout<<endl;
- for(int i=0,j=0;i<length;i++,j++)
- {
- Sum+=Arr[i];
- if(j==m-1) {
- cout<<"Summ in line "<<lineNumber<<" - "<<Sum<<endl;
- lineNumber++;
- Sum=0;
- j=-1;
- }
- }
- cout<<endl;
- }
- void columnSum()
- {
- cout<<endl;
- int *Sum=new int[m];
- for(int i=0;i<m;i++)
- {
- Sum[i]=0;
- }
- for(int i=0,j=0;i<length;i++,j++)
- {
- if (j==m) j=0;
- Sum[j]+=Arr[i];
- }
- for(int i=0;i<m;i++)
- {
- cout<<"Summ in column "<<i<<" - "<<Sum[i]<<endl;
- }
- cout<<endl;
- }
- };;
- int main()
- {
- int n=3;
- int m=4;
- MyArray a(n,m);
- srand((unsigned)time(0));
- for (int i=0;i<n;i++)
- for (int j=0;j<m;j++)
- a.ChangeElement(i,j,rand()%10);
- a.ChangeElement(2,2,0);
- a.Output();
- a.lineSum();
- a.columnSum();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement