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 is: 7


Example: How to find saddle point in a matrix in c

Run it Now
#include<stdio.h>

int main() {

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

   printf("Enter Size of Matrix :");
   scanf("%d", & num);

   printf("\nEnter 2D Array Elements :");
   for (i = 0; i < num; i++) {
       for (j = 0; j < num; j++) {
           scanf("%d", & a[i][j]);
       }
   }

   printf("\nElements of The 2D array is :");
   for (i = 0; i < num; i++) {
       printf("\n");
       for (j = 0; j < num; j++) {
           printf("%d ", a[i][j]);
       }
   }

   /* Logic start 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;
           }
       }

       large = 0;
       for (j = 0; j < num; j++) {
           if (large < a[j][p]) {
               large = a[j][p];
           }
       }
       if (sm == large) {
           printf("\nValue of Saddle Point :%d", sm);
           f = 0;
       }
   }

   if (f > 0)
       printf("\nNo Saddle Point ");

   return 0;
}

Output:

Enter Size of Matrix :3
Enter 2D Array Elements :20 30 40 56 78 45 1 2 3
Elements of The 2D array is :
20 30 40
56 78 45
1 2 3
Value of Saddle Point :45