-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiagonal.cpp
More file actions
128 lines (117 loc) · 1.97 KB
/
diagonal.cpp
File metadata and controls
128 lines (117 loc) · 1.97 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
#include<iostream>
using namespace std;
template<class T>
class diagonal
{
public:
diagonal(int N=10);
T get(int ,int )const;
void set(int ,int , T&);
private:
int n;
T *arr;
};
template<class T>
diagonal<T>::diagonal(int N)
{
if(N<1)
throw ("Matrix size must be greater than zero");
n=N;
arr=new T [n];
}
template<class T>
T diagonal<T>::get(int i,int j)const
{
if(i<0||j<0||i>n||j>n)
throw ("Matrix Index out of bounds");
if(i==j)
return arr[i];
else
return 0;
return 0;
}
template<class T>
void diagonal<T>::set(int i,int j, T& val)
{
if(i<0||j<0||i>n||j>n)
throw ("Matrix Index out of bounds");
if(i==j)
arr[i]=val;
else
{
if(val!=0)
{
cout<<"Value not zero enter again";
cin>>val;
val=0;
arr[i]=val;
}
}
}
int main()
{
int n;int val;
cout<<"Enter the order of matrix"<<endl;
cin>>n;int i,j;
diagonal<int> d (n);
int **arr;
arr=new int*[n];
for(int i=0;i<n;i++)
{
arr[i]=new int[n];
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<"element at position"<<i<<j<<endl;
cin>>arr[i][j];
d.set(i,j,arr[i][j]);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
int ch;char repeat;
do
{
cout<<"***MENU***"<<endl;
cout<<"1.Update a element"<<endl;
cout<<"2.Get an element"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Enter the index of the element"<<endl;
cin>>i>>j;
cout<<"Enter the value"<<endl;
cin>>val;
d.set(i,j,val);
break;
}
case 2:
{
cout<<"Enter the position of matrix you want to know the value of"<<endl;
cin>>i>>j;
cout<<"The value is "<<d.get(i,j)<<endl;
break;
}
default:
{
cout<<"Wrong choice"<<endl;
break;
}
}
cout<<"Do you want to continue(Y/N)?"<<endl;
cin>>repeat;
}
while(repeat=='y'||repeat=='Y');
return 0;
}