# Problem when searching for a specific element of a matrix

2

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.

asked by DDN 12.05.2016 в 16:50
source

1

The error is found in assuming that you have a saddle point and you dedicate yourself to iterate through the matrix until you find a point that does not meet the requirement and it should be the other way around:

``````int existe=0;

for(posFila=0; posFila<M && !existe; posFila++)
{
for(posColumna=0; posColumna<N && !existe; posColumna++)
{
existe = puntoSilla(mat, posFila, posColumna);
}
}
``````

Why? Basically because before iterating you have not found the saddle point, then `existe=1` is a big lie. The fact that `puntoSilla` returns 0 if the current point is not the one that is searched makes the first iteration `existe` go to value 0, which means that the algorithm does not keep searching ... you would only save if the point of chair is in the first cell.

These types of errors you would find quickly if you learned to use a code debugger. They are an essential tool for programming so I recommend you hurry to learn how to use them.

On the other hand, if you allow me the criticism, try to avoid redundant code. I explain. In this code, for example:

``````if(enc)
{
for(i=0; i<M && enc; i++)
{
if(mat[posFila][posColumna]<mat[i][posColumna])
enc=0;
}
}
else enc=0;
``````

We can eliminate both conditionals. The outside remains because the `for` has as a conditional that `enc` has a different value of 0 and we do not forget that the conditional of a `for` is executed for each iteration, the first included. In addition, the `else` is equally redundant since in your function `enc` can only be worth 1 or 0, then if it is not worth 1 it is clear that it will be worth 0.

To eliminate the second conditional one must understand that in C a condition returns 1 or 0 depending on whether it is true or false. If the intention is that `enc` is worth 0 if a higher value is found, it is enough to find a condition that is false only in that case.

The simplified equivalent would look like this:

``````for(i=0; i<M && enc; i++)
{
enc = (mat[posFila][posColumna]>=mat[i][posColumna]);
}
``````

And we could even avoid declaring the variables at the beginning of the function and having them inside the loop. In this way the variable ceases to exist at the end of the loop. It is not advisable to have live variables once they have fulfilled their mission:

``````for(int i=0; i<M && enc; i++)
{
enc = (mat[posFila][posColumna]>=mat[i][posColumna]);
}
``````

Greetings.