-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwodim_recur_22.cpp
78 lines (73 loc) · 1.77 KB
/
twodim_recur_22.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
// Diagonal of a Sqaure Matrix using Recursion
#include <iostream>
int input(int[][10], int, int);
void display(int, int, int[][10]);
int showDiagonalOfMatrix(int[][10], int, int, int, int);
using namespace std;
int main()
{
int a[10][10];
int m, n;
cout << "Enter row for Matrix A: " << endl;
cin >> m;
cout << "Enter columns for Matrix A: " << endl;
cin >> n;
if (m == n)
{
cout << "Enter the elements of Matrix A: " << endl;
input(a, m, n);
cout << "Displaying the Matrix A: " << endl;
display(m, n, a);
cout << "Displaying the Diagonal Of A Matrix: " << endl;
showDiagonalOfMatrix(a, m, n, 0, 0);
}
else
{
cout << "Matrix is not square matrix" << 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 showDiagonalOfMatrix(int arr[][10], int rows, int columns, int i, int j)
{
if (i == rows)
{
return 0;
}
if (j == columns)
{
cout << endl;
return showDiagonalOfMatrix(arr, rows, columns, i + 1, 0);
}
if (i==j)
{
cout << arr[i][j] << " ";
}
else{
cout << " ";
}
return showDiagonalOfMatrix(arr, rows, columns, i, j + 1);
}