forked from hariom20singh/data-structure-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalMatrix.cpp
More file actions
106 lines (91 loc) · 2.23 KB
/
DiagonalMatrix.cpp
File metadata and controls
106 lines (91 loc) · 2.23 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
// Matrix that have quite a lot of 0's in them, for e.g. Diagonal Matrix
#include <iostream>
using namespace std;
class DiagonalMatrix{
private:
int n;
int*A;
public:
DiagonalMatrix(int size){
n=size;
A=new int[size];
init();
}
void init(){
for(int i=0; i<n; i++){
A[i]=0;
}
}
void set(int i, int j, int x){
if(i==j && i<=n)
A[i-1]=x;
}
int get(int i, int j){
if(i>n || j>n)
return -1;
if(i==j)
return A[i-1];
return 0;
}
void display(){
cout<<"\nMatrix : "<<endl;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(i==j)
cout<<A[i]<<" ";
else
cout<<"0 ";
}
cout<<endl;
}
}
int getDimension(){
return n;
}
~DiagonalMatrix(){
delete []A;
}
};
int main()
{
int n;
cout<<"\nEnter Size of Diagonal : ";
cin>>n;
DiagonalMatrix D(n);
cout<<"Menu : ";
cout<<"\n\t1.Display\n\t2.Set Term\n\t3.Get Term\n\t4.Get Dimensions";
int opt;
do{
cout<<"\nChoose Option : ";
cin>>opt;
switch (opt)
{
case 0:
break;
case 1:
D.display();
break;
case 2:
int i,j,x;
cout<<"\nEnter Index : ";
cin>>i>>j;
cout<<"\nEnter Value : ";
cin>>x;
D.set(i, j, x);
break;
case 3:
int a,b;
cout<<"\nEnter Index : ";
cin>>a>>b;
cout<<"\nTerm : "<<D.get(a,b);
break;
case 4:
cout<<"\nDimensions of Matrix : "<<D.getDimension()<<"x"<<D.getDimension();
break;
default:
cout<<"\nInvalid Option";
break;
}
}while(opt);
return 0;
}