-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwodim_recur_3.cpp
98 lines (91 loc) · 2.6 KB
/
twodim_recur_3.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
// Subtraction of 2D array through recursion
#include <iostream>
void rules();
int input(int[][10], int, int);
void display(int, int, int[][10]);
int subTract(int[][10], int[][10], int[][10], int, int, int, int);
using namespace std;
int main()
{
int mat1[10][10], mat2[10][10], result[10][10];
int rowsize;
int colsize;
int i, j, k;
rules();
cout << "Enter row size" << endl;
cin >> rowsize;
cout << "Enter column size" << endl;
cin >> colsize;
if (rowsize == colsize)
{
cout << "Enter elements in 1st Matrix: " << endl;
input(mat1, rowsize, colsize);
cout << "Enter elements in 2nd Matrix: " << endl;
input(mat2, rowsize, colsize);
cout << "Displaying 1st Matrix:" << endl;
display(rowsize, colsize, mat1);
cout << "Displaying 2nd Matrix:" << endl;
display(rowsize, colsize, mat2);
subTract(mat1, mat2, result, rowsize, colsize, 0, 0);
cout << "Displaying Resultant Matrix:" << endl;
display(rowsize, colsize, result);
}
else
{
cout << "Row size and column size must be equal , else addition is not possible" << endl;
}
return 0;
}
void rules()
{
cout << "Rule: rows and columns must be equal to perform addition of two matrices. " << endl;
cout << "Hence column size and row size must be equal " << endl;
cout << " (m x n) +( m x n) format where, m=m and n=n"
<< "\n";
cout << "And m = number of rows and n = number of columns"
<< "\n\n";
}
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 << "|";
cout << endl;
}
}
int subTract(int mat1[][10], int mat2[][10], int result[][10], int rows, int columns, int i, int j)
{
if (i == rows)
{
return result[rows][columns];
}
if (i < rows)
{
if (j < columns)
{
result[i][j] = mat1[i][j] - mat2[i][j];
subTract(mat1, mat2, result, rows, columns, i, j + 1);
}
else
{
subTract(mat1, mat2, result, rows, columns, i + 1, 0);
}
}
}