-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagonalmatrix2d.cpp
80 lines (73 loc) · 1.3 KB
/
diagonalmatrix2d.cpp
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
//Program to create and display diagonal matrix using 2d array
//Vishruth codes
#include<iostream>
#include<stdio.h>
using namespace std;
class Matrix
{
private:
int A[10][10];
//dimension of the current matrix
int n;
public:
//to set the elements of the matrix
//Constructor
Matrix(int x[],int dim)
{
n=dim;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{
A[i][j]=x[i];
}
else
{
A[i][j]=0;
}
}
}
}
//just to get single elements from matrix
int get(int i, int j)
{
return (A[i][j]);
}
//to display the elements of the matrix
void display()
{
cout<<"\nThe elements are:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<A[i][j]<<" ";
}
printf("\n");
}
}
~Matrix()
{
cout << "\nDestructor called.\n";
}
};
int main()
{
cout<<"\n--| Diagonal matrix in C++ |--";
int n;
cout<<"\n\nEnter the dimension: ";
cin>>n;
int str[n];
cout<<"\nEnter the diagonal elements (separated by space): ";
for(int i=0;i<n;i++)
{
cin>>str[i];
}
//setting up the matrix
Matrix m(str,n);
//display the matrix
m.display();
return 0;
}