Saturday, October 20, 2018

Saddle point

/*
An m x n matrix is said to have a saddle point if some entry a[i][j] is the smallest value in
row i and the largest value in j. Write C/ C++ function that determines the location of a
saddle point if one exists.

*/

#include<stdio.h>
int main()
{
    int a[10][10],i,j,k,n,min,max,col,m;
   
    printf("enter order m,n of mxn matrix : ");
    scanf("%d %d",&m,&n);
    printf("enter elements row-wise\n");
   
    for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        scanf("%d",&a[i][j]);
                }
        }
       
        printf("\nMatrix is:\n");
        for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        printf("%d",a[i][j]);
                        printf("\t");
                }
                   printf("\n");
        }
       
       
    for(i=0;i<m;i++)
        {
                min=a[i][0];
                for(j=0;j<n;j++)
                {
                        if(a[i][j]<=min)
                        {
                                min=a[i][j];
                                col=j;
                        }
                }
                max=a[0][col];
                for(k=0;k<m;k++)
                {
                        if(a[k][col]>=max)
                        {
                                max=a[k][col];
                        }
                }
               
                if(max==min)
                {
            printf("\nsaddle point is %d\n",max);
                }          
        }
return 0;
}
/*
output-

enter order m,n of mxn matrix : 3 3
enter elements row-wise
1 2 3
4 5 6
7 8 9

Matrix is:
1    2    3   
4    5    6   
7    8    9   

saddle point is 7

*/

No comments:

Post a Comment