File tree 1 file changed +51
-59
lines changed
1 file changed +51
-59
lines changed Original file line number Diff line number Diff line change 1
- // Diagonal Matrix CPP
2
1
#include < iostream>
3
2
using namespace std ;
4
- class Diagonal
3
+ class Diagonal // Difining a diagonal class
5
4
{
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;}
5
+ private: // passing datamembers with private
6
+ int n; // Dimension
7
+ int *A; // array as pointer
8
+ public: // inside public the very 1st thing is constructor
9
+ Diagonal () // non parametrized constructor
10
+ {
11
+ n=2 ;
12
+ A= new int [2 ];
13
+ }
14
+ Diagonal (int n) // parametarized constructor
15
+ {
16
+ this ->n =n;
17
+ A=new int [n];
18
+ }
19
+ ~Diagonal () // since we are creating memory in heap so for realissing memory we will use destructor
20
+ {
21
+ delete [] A;
22
+ }
23
+ void Set (int i, int j, int x);
24
+ int Get (int i, int j);
25
+ void Display ();
28
26
};
29
27
void Diagonal::Set (int i,int j,int x)
30
28
{
31
- if (i==j)
32
- A[i-1 ]=x;
29
+ if (i==j)
30
+ A[i-1 ]=x;
33
31
}
34
32
int Diagonal::Get (int i,int j)
35
33
{
36
- if (i==j)
37
- return A[i-1 ];
38
- return 0 ;
34
+ if (i==j)
35
+ {
36
+ return A[i-1 ];
37
+
38
+ }
39
+ else return 0 ;
39
40
}
40
41
void Diagonal::Display ()
42
+
41
43
{
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 ()
44
+ for (int i=0 ;i<n;i++)
55
45
{
56
- int d;
57
- cout<<" Enter Dimensions" ;
58
- cin>>d;
59
-
60
- Diagonal dm (d);
46
+ for (int j=0 ;j<n;j++)
47
+ {
48
+ if (i==j)
49
+ cout << A[i]<<" " ;
50
+ else
51
+ cout<<" 0 " ;
52
+ }
53
+ cout<<endl;
54
+ }
55
+ }
61
56
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
- }
57
+ int main ()
72
58
73
- dm.Display ();
74
- return 0 ;
59
+ {
60
+ Diagonal d (4 );
61
+ d.Set (1 ,1 ,5 );
62
+ d.Set (2 ,2 ,9 );
63
+ d.Set (3 ,3 ,7 );
64
+ d.Set (4 ,4 ,2 );
65
+ d.Display ();
66
+ return 0 ;
75
67
}
You can’t perform that action at this time.
0 commit comments