-
Notifications
You must be signed in to change notification settings - Fork 56
/
diagonal matrix.cpp
85 lines (81 loc) · 1.52 KB
/
diagonal matrix.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
81
82
83
84
85
#include<iostream>
using namespace std;
void getinput(int** arr, int r, int c)
{
cout << " Enter the entries of matrix : " << endl;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> arr[i][j];
}
}
}
void displaymatrix(int** arr, int r, int c)
{
cout << " Display of matrix : " << endl;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout << arr[i][j]<<" ";
}
cout << endl;
}
}
void check(int** arr, int r, int c)
{
int count = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (i != j && arr[i][j] != 0)
{
count = 1;
}
}
}
if (count == 1)
{
cout << " Matrix is not diagonal matrix " << endl;
}
else
cout << " Matrix is a diagonal matrix " << endl;
}
void deallocatearray(int** arr, int r, int c)
{
for (int i = 0; i < c; i++)
{
delete[]arr[i];
}
delete[]arr;
arr = NULL;
}
int main()
{
int rows;
cout << " Enter the no. of rows: " << endl;
cin >> rows;
int cols;
cout << " Enter the no. of columns: " << endl;
cin >> cols;
while (rows != cols)
{
cout << " ERROR! Enter again :" << endl;
cout << " Enter the no. of columns: " << endl;
cin >> cols;
cout << " Enter the no. of rows: " << endl;
cin >> rows;
}
int** arr = new int* [cols];
for (int i = 0; i < cols; i++)
{
arr[i] = new int[rows];
}
getinput(arr, rows, cols);
displaymatrix(arr, rows, cols);
check(arr, rows, cols);
deallocatearray(arr, rows, cols);
return 0;
}