File tree 1 file changed +75
-0
lines changed
1 file changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Diagonal Matrix CPP
2
+ #include < iostream>
3
+ using namespace std ;
4
+ class Diagonal
5
+ {
6
+ private:
7
+ int *A;
8
+ int n;
9
+ public:
10
+ Diagonal ()
11
+ {
12
+ n=2 ;
13
+ A=new int [2 ];
14
+ }
15
+ Diagonal (int n)
16
+ {
17
+ this ->n =n;
18
+ A=new int [n];
19
+ }
20
+ ~Diagonal ()
21
+ {
22
+ delete [] A;
23
+ }
24
+ void Set (int i,int j,int x);
25
+ int Get (int i,int j);
26
+ void Display ();
27
+ int GetDimension (){return n;}
28
+ };
29
+ void Diagonal::Set (int i,int j,int x)
30
+ {
31
+ if (i==j)
32
+ A[i-1 ]=x;
33
+ }
34
+ int Diagonal::Get (int i,int j)
35
+ {
36
+ if (i==j)
37
+ return A[i-1 ];
38
+ return 0 ;
39
+ }
40
+ void Diagonal::Display ()
41
+ {
42
+ for (int i=1 ;i<=n;i++)
43
+ {
44
+ for (int j=1 ;j<=n;j++)
45
+ {
46
+ if (i==j)
47
+ cout<<A[i-1 ]<<" " ;
48
+ else
49
+ cout<<" 0 " ;
50
+ }
51
+ cout<<endl;
52
+ }
53
+ }
54
+ int main ()
55
+ {
56
+ int d;
57
+ cout<<" Enter Dimensions" ;
58
+ cin>>d;
59
+
60
+ Diagonal dm (d);
61
+
62
+ int x;
63
+ cout<<" Enter All Elements" ;
64
+ for (int i=1 ;i<=d;i++)
65
+ {
66
+ for (int j=1 ;j<=d;j++)
67
+ {
68
+ cin>>x;
69
+ dm.Set (i,j,x);
70
+ }
71
+ }
72
+
73
+ dm.Display ();
74
+ return 0 ;
75
+ }
You can’t perform that action at this time.
0 commit comments