Skip to content

Commit e403bc7

Browse files
authored
Add files via upload
1 parent d0d93b1 commit e403bc7

File tree

1 file changed

+51
-59
lines changed

1 file changed

+51
-59
lines changed

Matrix/01 Diagonal matrix.cpp

+51-59
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,67 @@
1-
// Diagonal Matrix CPP
21
#include <iostream>
32
using namespace std;
4-
class Diagonal
3+
class Diagonal // Difining a diagonal class
54
{
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();
2826
};
2927
void Diagonal::Set(int i,int j,int x)
3028
{
31-
if(i==j)
32-
A[i-1]=x;
29+
if (i==j)
30+
A[i-1]=x;
3331
}
3432
int Diagonal::Get(int i,int j)
3533
{
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;
3940
}
4041
void Diagonal::Display()
42+
4143
{
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++)
5545
{
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+
}
6156

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 ()
7258

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;
7567
}

0 commit comments

Comments
 (0)