assignment_01.cpp
Problem Statement
assignment_01.cpp
Write a program to implement a Diagonal Matrix a Lower Triangular Matrix, an Upper Triangular Matrix, and a Symmetric Matrix using a one-dimensional array.
Source Code
cpp
#include <iostream>
using namespace std;
class Matrix
{
private:
int n, *arr;
public:
Matrix(int size)
{
n = size;
arr = new int[n * (n + 1) / 2];
}
void setDiagonal(int i, int j, int x)
{
if (i == j)
arr[i] = x;
}
void setLowerTri(int i, int j, int x)
{
if (i >= j)
arr[i * (i + 1) / 2 + j] = x;
}
void setUpperTri(int i, int j, int x)
{
if (i <= j)
arr[n * i - (i * (i - 1) / 2) + (j - i)] = x;
}
void setSymmetric(int i, int j, int x)
{
(i >= j) ? arr[i * (i + 1) / 2 + j] = x : arr[j * (j + 1) / 2 + i] = x;
}
int getDiagonal(int i, int j)
{
return (i == j) ? arr[i] : 0;
}
int getLowerTri(int i, int j)
{
return (i >= j) ? arr[i * (i + 1) / 2 + j] : 0;
}
int getUpperTri(int i, int j)
{
return (i <= j) ? arr[n * i - i * (i - 1) / 2 + (j - i)] : 0;
}
int getSymmetric(int i, int j)
{
return (i >= j) ? arr[i * (i + 1) / 2 + j] : arr[j * (j + 1) / 2 + i];
}
void display(int type)
{
cout << endl
<< "Matrix:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (type == 1)
cout << getDiagonal(i, j) << " ";
else if (type == 2)
cout << getLowerTri(i, j) << " ";
else if (type == 3)
cout << getUpperTri(i, j) << " ";
else if (type == 4)
cout << getSymmetric(i, j) << " ";
}
cout << endl;
}
}
~Matrix()
{
delete[] arr;
}
};
int main()
{
int n, choice, x;
cout << "Enter the order of the matrix : ";
cin >> n;
Matrix M(n);
cout << "\n1. Diagonal Matrix.";
cout << "\n2. Lower Triangular Matrix.";
cout << "\n3. Upper Triangular Matrix.";
cout << "\n4. Symmetric Matrix.";
cout << "\nEnter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\nEnter the diagonal elements : ";
for (int i = 0; i < n; i++)
{
cin >> x;
M.setDiagonal(i, i, x);
}
M.display(1);
break;
case 2:
cout << "\nEnter the elements : ";
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
cin >> x;
M.setLowerTri(i, j, x);
}
}
M.display(2);
break;
case 3:
cout << "\nEnter the elements : ";
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
cin >> x;
M.setUpperTri(i, j, x);
}
}
M.display(3);
break;
case 4:
cout << "\nEnter the elements : ";
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
cin >> x;
M.setSymmetric(i, j, x);
}
}
M.display(4);
break;
default:
cout << "Invalid Choice.";
}
return 0;
}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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
● Author - Amit Dutta