-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathSparseMatricesAdd.cpp
81 lines (75 loc) · 1002 Bytes
/
SparseMatricesAdd.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
#include<iostream.h>
#include<conio.h>
class sparse
{
struct sp
{
int r;
int c;
int v;
}
s[10];
public:
sparse()
{
s[0].r=0;
s[0].c=0;
s[0].v=0;
}
void create()
{
cout<<“\n no of rows & columns& elements”;
cin>>s[0].r;
cin>>s[0].c;
cin>>s[0].v;
for(int i=1;i<=s[0].v;i++)
{
cout<<“\n row & column & element”;
cin>>s[i].r;
cin>>s[i].c;
cin>>s[i].v;
}
}
void display()
{
cout<<“\n no of rows & columns& elements\n”;
for(int i=0;i<=s[0].v;i++)
{
cout<<s[i].r<<“\t”;
cout<<s[i].c<<“\t”;
cout<<s[i].v<<“\n”;
}
}
sparse operator +(sparse s2)
{
sparse s3;
int k=1;
s3.s[0].r=s[0].r;
s3.s[0].c=s[0].c;
int i=1,j=1;
while(i<=s[0].v || j<=s2.s[0].v)
{
if((s[i].r==s2.s[j].r)&&(s[i].c==s2.s[j].c))
{
s3.s[k].r=s[i].r;
s3.s[k].c=s[i].c;
s3.s[k].v=s[i].v+s2.s[j].v;
i++; j++; k++;
}
else if((s[i].r==s2.s[j].r)&&(s[i].c==s2.s[j].c))
}
s3.s[0].v=k;
return s3;
}
};
void main()
{
sparse s1,s2,s3;
s1.create();
s2.create();
s1.display();
s2.display();
s3=s1+s2;
s3.display();
getch();
}