forked from Ayush-chhabra/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriDiagonal.cpp
More file actions
117 lines (99 loc) · 1.9 KB
/
TriDiagonal.cpp
File metadata and controls
117 lines (99 loc) · 1.9 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
#include<iostream>
using namespace std;
class TriDiagonal
{
private:
int n;
int * A;
public:
TriDiagonal(int n)
{
this->n=n;
A=new int[3*n-2];
for(int i=0;i<3*n-2;i++)
{
A[i]=0;
}
}
void set(int i,int j,int x);
int get(int i,int j);
~TriDiagonal()
{
delete []A;
}
};
void TriDiagonal::set(int i,int j,int x)
{
if(i-j==1)
A[j-1]=x;
else if(i==j)
A[n-1+j-1]=x;
else if(i-j==-1)
A[n+n-1+j-2]=x;
}
int TriDiagonal::get(int i,int j)
{
if(i-j==1)
return A[j-1];
else if(i==j)
return A[n-1+j-1];
else if(i-j==-1)
return A[n+n-1+j-2];
else
return 0;
}
int main()
{
int n,ch,r,c,val;
cout<<"Enter the dimensions of the matrix : ";
cin>>n;
TriDiagonal * D = new TriDiagonal(n);
do
{
cout<<"\nMenu\n";
cout<<"1.Set\n";
cout<<"2.Get\n";
cout<<"3.Display\n";
cout<<"4.Exit\n";
cout<<"Enter the choice : ";
cin>>ch;
if (ch==1)
{
cout<<"Enter the row and column of the element:\n";
cin>>r>>c;
if (r<1||c<1||r>n||c>n)
{
cout<<"Incorrect values given";
continue;
}
cout<<"Enter the element : ";
cin>>val;
D->set(r,c,val);
}
else if (ch==2)
{
cout<<"Enter the row and column of the element:\n";
cin>>r>>c;
if (r<1||c<1||r>n||c>n)
{
cout<<"Incorrect values given";
continue;
}
cout<<"The element is : "<<D->get(r,c);
}
else if (ch==3)
{
cout<<"The matrix is :-\n";
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<D->get(i,j)<<" ";
cout<<endl;
}
}
else if(ch==4)
break;
else
cout<<"Wrong input!!";
} while (ch!=4);
}