forked from Anubhav-1020/Hacktoberfest-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayRepCLASS.cpp
111 lines (96 loc) · 2.74 KB
/
ArrayRepCLASS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <stdlib.h>
using namespace std;
class element{
public:
int i;
int j;
int x;
};
class sparce{
int m;
int n;
int num;
element *e;
public:
sparce(int m,int n,int num){
this->n=n;
this->m=m;
this->num=num;
e = new element [this->num];
}
~sparce(){
delete [] e;
}
friend istream & operator>>(istream &is , sparce &s); //void get();
friend ostream & operator<<(ostream &os , sparce &s); //void display();
sparce operator + (sparce &s); //sparce add(sparce s);
};
istream & operator>>(istream &is , sparce &s) //void sparce::get()
{
int i;
cout << "Enter non zero elements:- "<<endl;
for(i=0;i<s.num;i++){
cin>>s.e[i].i>>s.e[i].j>>s.e[i].x;
}
return is;
}
ostream & operator<<(ostream &os , sparce &s) //void sparce::display()
{
int i,j;
int k=0;
for(i=0;i<s.m;i++){
for(j=0;j<s.n;j++){
if(i==s.e[k].i && j==s.e[k].j)
cout<<s.e[k++].x<<" ";
else
cout<<"0 ";
}
cout<<endl;
}
return os;
}
sparce sparce :: operator + (sparce &s) //sparce sparce :: add(sparce s)
{
int i,j,k;
i=j=k=0;
if(n!=s.n || m!=s.m)
return sparce(0,0,0);
sparce *sum=new sparce(m,n,num+s.num);
while(i<num && j<s.num){
if(e[i].i<s.e[j].i)
sum->e[k++]=e[i++];
else if(e[i].i>s.e[j].i)
sum->e[k++]=s.e[j++];
else{
if(e[i].j<s.e[j].j)
sum->e[k++]=e[i++];
else if(e[i].j>s.e[j].j)
sum->e[k++]=s.e[j++];
else{
sum->e[k]=e[i];
sum->e[k++].x=e[i++].x+s.e[j++].x;
}
}
}
for(;i<num;i++)
sum->e[k++]=e[i];
for(;j<s.num;j++)
sum->e[k++]=s.e[j];
sum->num=k;
return *sum;
}
int main(){
sparce s1(3,4,3);
sparce s2(3,4,4);
cin>>s1; //s1.get();
cin>>s2; //s2.get();
cout<<"MATRIX 1 :- "<<endl;
cout<<s1; //s1.display();
cout<<"MATRIX 2 :- "<<endl;
cout<<s2; //s2.display();
sparce sum = s1 + s2; //s2.add(s1);
cout<<"MATRIX 3 (SUM) :- "<<endl;
cout<<sum<<endl; //sum.display();
return 0;
}