-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwodim_recur_10.cpp
166 lines (146 loc) · 4.17 KB
/
twodim_recur_10.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Calculate adjoint of a matrix using cofactors through matrix recursion.
#include <iostream>
int input(int[][10], int, int);
void display(int, int, int[][10]);
int getCofactor(int[][10], int[][10], int, int, int, int, int, int, int, int);
int displayCofact(int[][10], int, int, int, int, int);
int determinant(int[][10], int, int);
int adjointMatrix(int [][10], int [][10], int , int , int , int );
using namespace std;
int main()
{
cout << "This is for any higher order matrix" << endl;
int a[10][10], adj[10][10];
int m1, n1, i, j, k;
cout << "Enter row for Matrix : " << endl;
cin >> m1;
cout << "Enter columns for Matrix : " << endl;
cin >> n1;
if (m1 == n1)
{
cout << "Enter elements in Matrix: " << endl;
input(a, m1, n1);
cout << "Displaying Matrix:" << endl;
display(m1, n1, a);
cout << "Determinant of Matrix is: " << determinant(a, m1, n1) << endl;
cout<<"\n";
cout << "Cofactors of given above matrix" << endl;
displayCofact(a, m1, n1, 0, 0, 0);
cout <<"Ajoint of matrix is:" << endl;
adjointMatrix(a, adj, m1, n1, 0, 0);
display(m1, n1, adj);
}
else
{
cout << "Determinant of Matrix is not possible" << endl;
}
return 0;
}
int input(int array[][10], int rows, int columns)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << "arr[" << i << "][" << j << "] = ";
cin >> array[i][j];
}
}
return array[rows][10];
}
void display(int rows, int columns, int array[][10])
{
for (int i = 0; i < rows; i++)
{
cout << "|";
for (int j = 0; j < columns; j++)
{
cout << array[i][j] << " ";
}
cout << "|" << endl;
}
}
int getCofactor(int array[][10], int temp[][10], int rows, int columns, int p, int q, int i, int j, int s, int t)
{
if (i < rows)
{
if (j < columns)
{
if (i != p && j != q)
{
temp[s][t] = array[i][j];
t++;
if (t == columns - 1)
{
t = 0;
s++;
}
}
getCofactor(array, temp, rows, columns, p, q, i, j + 1, s, t);
}
else
{
getCofactor(array, temp, rows, columns, p, q, i + 1, 0, s, t);
}
}
}
int determinant(int array[][10], int rows, int columns)
{
int det = 0;
int temp[10][10];
int sign = 1;
if (rows == 1 && columns == 1)
{
return array[0][0];
}
for (int cofac = 0; cofac < rows; cofac++)
{
getCofactor(array, temp, rows, columns, 0, cofac, 0, 0, 0, 0);
det = det + (sign * array[0][cofac] * determinant(temp, rows - 1, columns - 1));
sign = -sign;
}
return det;
}
int displayCofact(int arr[][10], int rows, int columns, int i, int j, int count)
{
int temp[10][10];
if (i < rows)
{
if (j < columns)
{
getCofactor(arr, temp, rows, columns, i, j, 0, 0, 0, 0);
cout << "Cofactor: " << count << endl;
display(rows - 1, columns - 1, temp);
cout << endl;
displayCofact(arr, rows, columns, i, j + 1, count + 1);
}
else
{
displayCofact(arr, rows, columns, i + 1, 0, count + 1);
}
}
}
int adjointMatrix(int arr[][10],int adj[][10], int rows, int columns, int i, int j)
{
int temp[10][10];
int sign = 1;
if (i < rows)
{
if (j < columns)
{
getCofactor(arr, temp, rows, columns, i, j, 0, 0, 0, 0);
if((i+j)%2== 0){
sign = 1;
}
else{
sign = -1;
}
adj[j][i] = (sign)*(determinant(temp, rows - 1, columns - 1));
adjointMatrix(arr, adj, rows, columns, i, j + 1);
}
else
{
adjointMatrix(arr,adj, rows, columns, i + 1, 0);
}
}
}