Problem statement
Write a c program to find out saddle point of a metrics
For a given m × n matrix A, the (i, j) the element is said to be a saddle
point if it is the maximum in row i and minimum in column j, or
minimum in row i and maximum in column j.
(a) Write a function int saddle (A, i, j) to test if the (i, j) the element
of the given matrix A is a saddle point or not. Choose the
function prototype in a suitable way, and use additional parameters,
if necessary. If the element is a saddle point, the function
must return 1; if not, it must return 0.
(b) Write a main function that would first read in an m × n matrix
A. Then all the saddle points of the matrix (along with their
positions) would be printed, using the function saddle(). Both
the given matrix and also the output should be printed in properly
formatted form.
For a given m × n matrix A, the (i, j) the element is said to be a saddle
point if it is the maximum in row i and minimum in column j, or
minimum in row i and maximum in column j.
(a) Write a function int saddle (A, i, j) to test if the (i, j) the element
of the given matrix A is a saddle point or not. Choose the
function prototype in a suitable way, and use additional parameters,
if necessary. If the element is a saddle point, the function
must return 1; if not, it must return 0.
(b) Write a main function that would first read in an m × n matrix
A. Then all the saddle points of the matrix (along with their
positions) would be printed, using the function saddle(). Both
the given matrix and also the output should be printed in properly
formatted form.
(
For a given m × n matrix A, the (i, j) the element is said to be a saddle
point if it is the maximum in row i and minimum in column j, or
minimum in row i and maximum in column j.
)point if it is the maximum in row i and minimum in column j, or
minimum in row i and maximum in column j.
Run your program for the following two matrices:
7 5 12 17
8 16 9 22
3 20 7 30
5 11 54 20
12 12 12 12 12
12 20 20 20 20
12 5 5 5 5
#include<stdio.h>
int saddle(int a[5][5],int i,int j,int m,int n);
int main()
{
int temp_i,temp_j,m,n,a[5][5],y;
printf("enter the dimention of the metrics m*n resp. \n");
scanf("%d %d",&m,&n);
printf("now enter the array row wise \n");
for(temp_i=0;temp_i<m;temp_i++)
{
for(temp_j=0;temp_j<n;temp_j++)
{
scanf("%d",&a[temp_i][temp_j]);
}
}
printf("the array is read as \n");
for(temp_i=0;temp_i<m;temp_i++)
{
for(temp_j=0;temp_j<n;temp_j++)
{
printf("%d ",a[temp_i][temp_j]);
}
printf("\n");
}
printf("trying to calculate saddle points \n");
for(temp_i=0;temp_i<m;temp_i++)
{
for(temp_j=0;temp_j<n;temp_j++)
{
if( saddle(a,temp_i,temp_j,m,n)==1)
{
printf("{i=%d,j=%d}",temp_i,temp_j);
}
}
}
scanf("%d",&y);
return 0;
}
int saddle(int a[5][5],int i,int j,int m,int n)
{
int temp_i,temp_j;
for(temp_i=0;temp_i<m;temp_i++)
{
if(a[i][j]>a[temp_i][j])
{
return 0;
}
}
for(temp_j=0;temp_j<n;temp_j++)
{
if(a[i][j]<a[i][temp_j])
{
return 0;
}
}
return 1;
}
No comments:
Post a Comment