Repository navigation
Expand file tree
/
Copy pathAssignment3.cpp
More file actions
228 lines (220 loc) · 4.48 KB
/
Copy pathAssignment3.cpp
File metadata and controls
228 lines (220 loc) · 4.48 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
using namespace std;
class cSparse
{
private:
int nMat[20][3];
public:
void fget_sparse();
void fdisplay_sparse();
void fadd_sparse(cSparse , cSparse);
void stranspose_sparse(cSparse);
void ftranspose_sparse(cSparse);
};
void cSparse::fget_sparse()
{
cout<<"\nEnter number of Rows, columns, and number of non zero entries : ";
cin>>nMat[0][0]>>nMat[0][1]>>nMat[0][2];
for(int i=1;i<=nMat[0][2];i++)
{
cout<<"\nEnter the Row number : ";
cin>>nMat[i][0];
cout<<"\nEnter the Column number : ";
cin>>nMat[i][1];
cout<<"\nEnter the non zero value at that position : ";
cin>>nMat[i][2];
}
}
void cSparse::fdisplay_sparse()
{
cout<<"\n"<<nMat[0][0]<<"\t"<<nMat[0][1]<<"\t"<<nMat[0][2];
cout<<"\n------------------";
for(int i=1;i<=nMat[0][2];i++)
{
cout<<"\n"<<nMat[i][0]<<"\t"<<nMat[i][1]<<"\t"<<nMat[i][2];
}
}
void cSparse::fadd_sparse(cSparse a, cSparse b)
{
int p=0,q=0;
nMat[0][0]=a.nMat[0][0];
nMat[0][1]=a.nMat[0][1];
p=a.nMat[0][2];
q=b.nMat[0][2];
int i=1,j=1,k=1;
while(i<=p && j<=q)
{
if(a.nMat[i][0]==b.nMat[j][0])
{
if(a.nMat[i][1]==b.nMat[j][1])
{
nMat[k][0]=a.nMat[i][0];
nMat[k][1]=a.nMat[i][1];
nMat[k][2]=a.nMat[i][2] + b.nMat[j][2];
i++;j++;k++;
}
else
{
if(a.nMat[i][1]<b.nMat[j][1])
{
nMat[k][0]=a.nMat[i][0];
nMat[k][1]=a.nMat[i][1];
nMat[k][2]=a.nMat[i][2];
i++;
k++;
}
else
{
nMat[k][0]=b.nMat[j][0];
nMat[k][1]=b.nMat[j][1];
nMat[k][2]=b.nMat[j][2];
j++;k++;
}
}
}
else
{
if(a.nMat[i][0]<b.nMat[j][0])
{
nMat[k][0]=a.nMat[i][0];
nMat[k][1]=a.nMat[i][1];
nMat[k][2]=a.nMat[i][2];
i++;
k++;
}
else
{
nMat[k][0]=b.nMat[j][0];
nMat[k][1]=b.nMat[j][1];
nMat[k][2]=b.nMat[j][2];
j++;k++;
}
}
}
while(i<=p)
{
nMat[k][0]=a.nMat[i][0];
nMat[k][1]=a.nMat[i][1];
nMat[k][2]=a.nMat[i][2];
i++;
k++;
}
while(j<=q)
{
nMat[k][0]=b.nMat[j][0];
nMat[k][1]=b.nMat[j][1];
nMat[k][2]=b.nMat[j][2];
j++;k++;
}
nMat[0][2]=k-1;
}
void cSparse::stranspose_sparse(cSparse b)
{
int k=1;
nMat[0][0]=b.nMat[0][1];
nMat[0][1]=b.nMat[0][0];
nMat[0][2]=b.nMat[0][2];
if(b.nMat[0][2]>0)
{
for(int i=0;i<b.nMat[0][1];i++)
{
for(int j=1;j<=b.nMat[0][2];j++)
{
if(i==b.nMat[j][1])
{
nMat[k][0]=b.nMat[j][1];
nMat[k][1]=b.nMat[j][0];
nMat[k][2]=b.nMat[j][2];
k++;
}
}
}
}
else{
cout<<"Empty Matrix";
}
}
void cSparse::ftranspose_sparse(cSparse a)
{
int count[10],index[10];
nMat[0][0]=a.nMat[0][1];
nMat[0][1]=a.nMat[0][0];
nMat[0][2]=a.nMat[0][2];
for(int i=0;i<a.nMat[0][1];i++)
{
count[i]=0;
}
for(int j=1;j<=a.nMat[0][2];j++)
{
count[a.nMat[j][1]]++;
}
index[0]=1;
for(int i=1;i<a.nMat[0][1];i++)
{
index[i]=index[i-1]+count[i-1];
}
for(int k=1;k<=a.nMat[0][2];k++)
{
int pstn=0,p=0;
pstn=a.nMat[k][1];
p=index[pstn];
nMat[p][0]=a.nMat[k][1];
nMat[p][1]=a.nMat[k][0];
nMat[p][2]=a.nMat[k][2];
index[pstn]++;
}
}
int main()
{
cSparse s1,s2,s3,s4,s5;
char ch;
int n;
do
{
cout<<"\npress 1 for sparse matrix addition";
cout<<"\npress 2 for simple transpose of the sparse matrix";
cout<<"\npress 3 for fast transpose of the sparse matrix";
cout<<"\nEnter your choice";
cin>>n;
switch(n)
{
case 1:
cout<<"\nEnter the entries of sparse matrix A : ";
s1.fget_sparse();
cout<<"\nsparse matrix representation of A : ";
s1.fdisplay_sparse();
cout<<"\nEnter the entries of sparse matrix B : ";
s2.fget_sparse();
cout<<"\nsparse matrix representation of B : ";
s2.fdisplay_sparse();
s3.fadd_sparse(s1,s2);
cout<<"\nAddition of two Entered sparse matrices is : ";
s3.fdisplay_sparse();
break;
case 2:
cout<<"\nEnter the entries of sparse matrix : ";
s1.fget_sparse();
cout<<"\nsparse matrix representation : ";
s1.fdisplay_sparse();
s4.stranspose_sparse(s1);
cout<<"\nSimple transpose of the 1st matrix is : ";
s4.fdisplay_sparse();
break;
case 3:
cout<<"\nEnter the entries of sparse matrix : ";
s1.fget_sparse();
cout<<"\nsparse matrix representation : ";
s1.fdisplay_sparse();
s5.ftranspose_sparse(s1);
cout<<"\nfast transpose of the 1st matrix is : ";
s5.fdisplay_sparse();
break;
default:
cout<<"\nEnter valid choice";
}
cout<<"\nWhether you wish to continue (Y/N) : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<"THANK YOU " ;
return 0;
}