-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonalMatrix.cpp
More file actions
78 lines (70 loc) · 1.28 KB
/
diagonalMatrix.cpp
File metadata and controls
78 lines (70 loc) · 1.28 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
/**
* @file diagonalMatrix.cpp
* @author Abhishek
* @brief Hee we implement diagonal matrix but use single dimension array to store it to save space.
* @version 0.1
* @date 2022-04-23
*
* @copyright Copyright (c) 2022
*
*/
#include <iostream>
class Diagonal
{
private:
int n;
int *A;
public:
//Storing 2D diagonal matrix as a single dimnesion array.
Diagonal(int n)
{
this->n = n;
A = new int[n];
}
void set(int i, int j, int x);
int get(int i, int j);
void display();
~Diagonal()
{
delete []A;
}
};
void Diagonal::set(int i, int j, int x)
{
if(i == j)
{
A[i-1] = x;
}
}
int Diagonal::get(int i, int j)
{
if(i != j)
return 0;
else
return A[i-1];
}
void Diagonal::display()
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i != j)
std::cout << 0 << " ";
else
std::cout << A[i] << " ";
}
std::cout << std::endl;
}
}
int main(int argc, char const *argv[])
{
Diagonal dMatrix(4);
dMatrix.set(1, 1, 10);
dMatrix.set(2, 2, 20);
dMatrix.set(3, 3, 30);
dMatrix.set(4, 4, 40);
dMatrix.display();
dMatrix.get(3,2);
return 0;
}