Advertisement

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 a saddle point in a matrix in C++

#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 << "Enter 2D array elements:";
    for (i = 0; i < num; i++) {
        for (j = 0; j < num; j++) {
            cin >> a[i][j];
        }
    }

    cout << "2D 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:1 2 3 4 5 6 7 8 9
2D Array List is :

1 2 3 
4 5 6 
7 8 9 

Value of Saddle Point:7