-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseMatrix.cpp
More file actions
93 lines (83 loc) · 1.45 KB
/
Copy pathparseMatrix.cpp
File metadata and controls
93 lines (83 loc) · 1.45 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
#include<iostream>
//#define r 5
//#define c 5
using namespace std;
template<class T>
class sparse;
template<class T>
class term
{
private:
int row,col;
T val;
public:
friend class sparse<T>;
term(){}
term(int r, int c, T v)
{
row = r;
col = c;
val = v;
}
void append(int r, int c, T v)
{
row = r;
col = c;
val = v;
}
void display()
{
cout<<"\n"<<row<<"\t"<<col<<"\t"<<val;
}
};
template<class T>
class sparse
{
private:
int maxsize;
int Not;
int nrows;
int ncols;
term<T> *element;
public:
sparse(int size)
{
maxsize = size;
Not=nrows=ncols=0;
element = new term<T>[maxsize];
}
void setsparse(const int a[5][5], int r, int c);
void display();
};
template<class T>
void sparse<T>::setsparse(const int a[5][5], int r, int c)
{
nrows = r;
ncols = c;
int count = 0;
cout<<"The sparse matrix is:\n";
cout<<"Row"<<"\tCol"<<"\tElement"<<endl;
for(int i=0; i<r; i++)
for(int j=0; j<c;j++)
if(a[i][j]!=0)
element[count++].append(i,j,a[i][j]);
Not = count;
}
template<class T>
void sparse<T>::display()
{
cout<<"number of rows:"<<nrows<<"\nnumber of columns: "<<ncols<<"\nNumber of terms: "<<Not<<endl;
cout<<"The sparse matrix is:\n";
cout<<"Row"<<"\tCol"<<"\tElement"<<endl;
for(int i=0; i<Not; i++)
element[i].display();
cout<<endl;
}
int main()
{
int arr[5][5] = {0,2,0,0,0 ,0,0,0,10,0 ,0,0,0,0,7 ,0,2,0,0,1, 2,0,0,0,0};
sparse<int> s1(10);
s1.setsparse(arr,5,5);
s1.display();
return 0;
}