C++ Program to find saddle point in a matrix


In this program, You will learn how to find saddle point in a matrix in C++.


4 5 6

7 8 9

5 1 3

Saddle Point :7


Example: How to find saddle point in a matrix in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int a[10][10], i, j, num;
    int sm, p, larg, f = 1;

    cout << "Enter Size of Matrix :";
    cin>>num;

    cout << "\nEnter 2D Array Elements :";
    for (i = 0; i < num; i++) {
        for (j = 0; j < num; j++) {
            cin >> a[i][j];
        }
    }

    cout << "\n2D Array List is :\n\n";
    for (i = 0; i < num; i++) {

        for (j = 0; j < num; j++) {
            cout << a[i][j] << " ";
        }
        cout << "\n";
    }

    /* Logic start from here */
    for (i = 0; i < num; i++) {
        p = 0;
        sm = a[i][0];
        for (j = 0; j < num; j++) {
            if (sm >= a[i][j]) {
                sm = a[i][j];
                p = j;
            }
        }
        larg = 0;
        for (j = 0; j < num; j++) {
            if (larg < a[j][p]) {
                larg = a[j][p];
            }
        }
        if (sm == larg) {
            cout << "\nValue of Saddle Point :" << sm;
            f = 0;
        }
    }

    if (f > 0) {
        cout << "\nNo Saddle Point ";
    }

    return 0;
}

Output:

Enter Size of Matrix :3
Enter 2D Array Elements :4 5 6 7 8 9 5 1 3
2D Array List is :

4 5 6
7 8 9
5 1 3

Value of Saddle Point :7