I'm going through a matrix of integers that must find a specific element (a saddle point, whichever is the lowest in its row and the largest in its column).
The program works well if I use a for loop to traverse the matrix that issues a message for each position. My problem is that I want you to just write a message: if you have found a saddle point or if you have not found one.
#include <stdio.h>
#define M 3
#define N 4
void leeMat(int mat[M][N]);
int puntoSilla(int mat[M][N], int posFila, int posColumna);
void escribeMat(int mat[M][N]);
void main()
{
int mat[M][N]=
{
1, 4, 0, 3,
1, 3, 0, 7,
3, 3, 2, 3, //punto de silla en posicion 2-2
}, posFila, posColumna, existe=1;
printf("\n");
escribeMat(mat);
for(posFila=0; posFila<M && existe; posFila++)
{
for(posColumna=0; posColumna<N && existe; posColumna++)
{
if(puntoSilla(mat, posFila, posColumna)!=1)
existe=0;
}
}
printf("\n");
if(existe)
printf("Tiene punto de silla en la posicion %d-%d\n", posFila, posColumna);
else printf("No tiene punto de silla\n");
}
void leeMat(int mat[M][N])
{
int i, j;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("Escribe valor para la posicion %d-%d: ", i, j);
scanf("%d", &mat[i][j]);
}
}
}
int puntoSilla(int mat[M][N], int posFila, int posColumna)
{
int i, j, enc=1;
for(j=0; j<N && enc; j++)
{
if(mat[posFila][posColumna]>mat[posFila][j])
enc=0;
}
if(enc)
{
for(i=0; i<M && enc; i++)
{
if(mat[posFila][posColumna]<mat[i][posColumna])
enc=0;
}
}
else enc=0;
return enc;
}
void escribeMat(int mat[M][N])
{
int i, j;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
printf("%4d", mat[i][j]);
printf("\n");
}
}
What I am explaining is in the main function. If anyone knew what the problem is, I would be very grateful. Thanks.