An element of a matrix is said to be \"Saddle Point\" if it is the strictly larg
ID: 3817798 • Letter: A
Question
An element of a matrix is said to be "Saddle Point" if it is the strictly largest element (max) in its row and the strictly smallest element (min) in its column. For example, the matrix shown below has 2 saddle points: (A matrix may have 0 - 2 saddle points) [10 12 7 3 12 3 10 6 2 8 18 24 17 6 10 15 21 10 8 12 1 18 22 4 15] Please write a C program that ask the user to input any matrix (size of matrix, and all elements in matrix), and output any saddle point(s) in the matrix (output the value and position of the saddle points).Explanation / Answer
Program for saddle point based on above description of saddle point
#include<stdio.h>
int main()
{
int n,a[100][100]; //Assuming maximum size of matrix is 100, please change the value if size of more than 100 is required
printf("Enter size of matrix: ");
scanf("%d",&n);
printf("Please enter the numbers: ");
int i,j,k,min,max,p[2][2];
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
max = a[i][0];
//Calculating maximum element of the row
for(j=0;j<n;j++)
{
if(a[i][j]>max)
{
max = a[i][j];
p[0][0]=i;
p[0][1]=j;
}
}
j=p[0][1];
min = a[0][j];
//Calculating the minimum element of the column
for(k=0;k<n;k++)
{
if(a[k][j]<min)
{
min = a[k][j];
p[1][0] = k;
p[1][1] = j;
}
}
//Print Saddle point - strictly largest element (max) in its row and striclty smallest element (min) in its column
if(min == max)
{
if(p[0][0]==p[1][0]&&p[0][1]==p[1][1])
{
printf("Saddle point is %d and its position is (%d,%d) ",min,p[0][0],p[0][1]);
}
}
}
return 0;
}
Sample Output
Enter size of matrix: 5
Please enter the numbers:
10 12 7 3 12 3 10 6 2 8 18 24 17 6 10 15 21 10 13 12 1 18 22 4 15
Saddle point is 10 and its position is (1,1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.