forked from arin2002/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2D Dynamic Array
86 lines (86 loc) · 1.64 KB
/
2D Dynamic Array
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
#include<iostream>
using namespace std;
int** create2darray(const int m, const int n);
void inputarray(int** inputarray, const int m, const int n);
void outputarray(int** arr, const int m, const int n);
bool checkdiagnol(int** arr, const int m, const int n);
void destructor(int** arr, const int m, const int n);
int main()
{
int m, n;
cout << "enter array rows and col : \n";
cin >> m >> n;
int** array = create2darray(m, n);
inputarray(array, m, n);
outputarray(array, m, n);
bool flag = checkdiagnol(array, m, n);
destructor(array, m, n);
array = nullptr;
if (flag)
{
cout << "It's a diagonal matrix.";
}
else
{
cout << "It's not a diagonal matrix.";
}
return 0;
}
int** create2darray(const int m, const int n)
{
int** array = new int* [m];
for (int i = 0; i < m; i++)
{
array[i] = new int[n];
}
return array;
}
void inputarray(int** inputarray, const int m, const int n)
{
cout << "Enter elements of array : \n";
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << "Enter element at position" << i << j << ": ";
cin >> inputarray[i][j];
}
}
}
void outputarray(int** arr, const int m, const int n)
{
cout << "Array is:" << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
bool checkdiagnol(int** arr, const int m, const int n)
{
if (m != n)
{
return false;
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i != j && arr[i][j] != 0)
{
return false;
}
}
}
return true;
}
void destructor(int** arr, const int m, const int n)
{
for (int i = 0; i < m; i++)
{
delete[] arr[i];
} delete[] arr;
}