Wednesday, 27 April 2011

Saddle point of a matrix in C


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i=0,j=0,k=0,r=0,c=0,max=0,min=0,col=0,row=0;
int **a;
printf("\nPlease enter the order of the first matrix::\n");
scanf("%d",&r);
printf("X\n");
scanf("%d",&c);
a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
{
a[i]=(int*)malloc(c*sizeof(int));
}
printf("\nEnter the element of the matrix::\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}


printf("\n\nThe Matrix is given bellow::\n\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
  for(i=0;i<r;i++)
{
max=a[i][0];
for(j=0;j<c;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
col=j;

}

}
min=a[i][col];
for(k=0;k<r;k++)
{
if(a[k][col]<min)
{
min=a[k][col];
row=k;
}
}
if(i==row)
{
printf("\n\nSaddle point found: value -> %d, found at %dX%d\n\n",a[i][col],(row+1),(col+1));
}

}
       getch();
       return 0;
}

No comments:

Post a Comment