-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaddlePoint.cpp
96 lines (81 loc) · 1.88 KB
/
SaddlePoint.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
#include<iostream>
using namespace std;
class smat {
int mat[10][10], i, j, row, col, min, max;
public:
smat() {
i = j = row = col = 0;
min = max = 0;
}
void getdata();
void showmat();
void saddlepoint();
};
void smat::getdata() {
cout << "\nEnter how many rows in matrix: ";
cin >> row;
cout << "\nEnter how many columns in matrix: ";
cin >> col;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << "\nEnter matrix mat[" << i << "][" << j << "] element: ";
cin >> mat[i][j];
}
}
}
void smat::showmat() {
cout << "\nThe elements inside the matrix are as follows:\n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << "\t" << mat[i][j];
}
cout << "\n";
}
cout << "\n";
}
void smat::saddlepoint() {
int big[5], small[5];
for (i = 0; i < 5; i++) {
big[i] = small[i] = 0;
}
for (i = 0; i < row; i++) {
small[i] = mat[i][0];
for (j = 0; j < col; j++) {
if (mat[i][j] <= small[i]) {
small[i] = mat[i][j];
}
}
}
for (j = 0; j < col; j++) {
big[j] = mat[0][j];
for (i = 0; i < row; i++) {
if (mat[i][j] >= big[j]) {
big[j] = mat[i][j];
}
}
}
max = small[0];
for (i = 0; i < row; i++) {
if (small[i] >= max) {
max = small[i];
}
}
min = big[0];
for (j = 0; j < col; j++) {
if (big[j] <= min) {
min = big[j];
}
}
if (min == max) {
cout << "\nSaddle point " << min << " is present in the matrix.";
} else {
cout << "\nSaddle point is not present in given matrix.";
}
}
int main() {
smat s1;
s1.getdata();
s1.showmat();
s1.saddlepoint();
return 0;
}