-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwodim_recur_9.cpp
104 lines (98 loc) · 2.69 KB
/
twodim_recur_9.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
//Display all cofactors using 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);
using namespace std;
int main()
{
cout << "This is for any higher order matrix" << endl;
int a[10][10], temp[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 << "Cofactors of given above matrix" << endl;
displayCofact(a, m1, n1, 0, 0, 0);
}
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 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);
}
}
}