i have a problem with a program which gets a matrix[3][3] (with values 1-20.) i want to print the index of cells whose surrounding cells all have lower values. in a matrix
0 0 0
0 1 0
0 0 0
it supposed to print only [1][1].
can someone look at my check_mat function an tell me how to repair it? thanks in advance.

``````#include <stdio.h>
#define N 3
#define M 3
int IsInside (int i, int r);
int GetNum ();
void get_mat (int mat[N][N]);
void print_mat (int mat[N][N]);
int sum_mat (int mat[N][N], int i, int j);
int check_mat (int mat[N][M], int i, int j);

void main()

{
int mat[N][N];
int i,j;

printf("%Please, insert %d nums, between 1-10\n", N*N);
get_mat(mat);
print_mat(mat);

for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(check_mat,i,j)
printf("[%d][%d]\n",i,j);
}
}
//print_mat(mat);
}

int IsInside (int r, int c)   //checks if the rows/colums are valid
{
if(r<0 || r>N || c<0 || c>N)
return 0;
return 1;
}

int GetNum ()
{
int num;
int check = 0;

do
{
if (check)
printf("error! between 1-10. try again");
scanf("%d", &num);
check++;

} while (num<0||num>10);

return num;

}

void print_mat (int mat[N][N])
{
int r, c;

for (r = 0; r < N; r++)
{
for (c = 0; c < N; c++)
{
printf("%3d", mat[r][c]);
}
printf("\n");
}
printf("\n\n");
}

void get_mat (int mat[N][N])
{
int r,c;

for(r=0;r<N;r++)
for(c=0;c<N;c++)
mat[r][c] = GetNum();
}

int check_mat (int mat[N][M], int i, int j)
{
int r,c;
int flag = 1;

for(r=i-1; r<i+1 ; r++)
{
for(c=j-1; c<j+1 ; j++)

if(mat[i][j]>mat[r][c]&&IsInside(r,c))
return 1;
return 0;

}

}``````

## All 9 Replies

Well you could try indenting your code.

The last return 0; seems WAY WAY out of place...

``````int check_mat (int mat[N][M], int i, int j)
{
int r,c;
int flag = 1;

for(r=i-1; r<i+1 ; r++)
{
for(c=j-1; c<j+1 ; j++)

if(mat[i][j]>mat[r][c]&&IsInside(r,c))
return 1;
return 0;

}

}``````

The for loop should be

``````for(r=i-1; r<=i+1 ; r++)
for(c=j-1; c<=j+1 ; j++)``````

Otherwise you will miss the last row/columns

Well you could try indenting your code.

The last return 0; seems WAY WAY out of place...

you right, fixed it

``````int check_mat (int mat[N][M], int i, int j)
{
int r,c;
for(r=i-1; r<i+1 ; r++)
{
for(c=j-1; c<j+1 ; j++)

if(!(mat[i][j]>mat[r][c]&&IsInside(r,c)))
return 0;
else
return 1;

}

}``````

plus found a STUPID typo in line 26. fixed it to
if(check_mat(mat,i,j))
*want to kick myself for not recognizing it earlier*

Are you sure you've fixed anything?

a) the code is still pretty messy (when posted on a forum). Your smart IDE may be able to understand mixed spaces and tabs, but pretty much everything else will make a mess of it at some point.

b) your for loops are useless, you return 0 or 1 after the first test.

``````int check_mat (int mat[N][M], int i, int j)
{
int r,c;
for(r=i-1; r<i+1 ; r++)
{
for(c=j-1; c<j+1 ; j++)
if(!(mat[i][j]>mat[r][c]&&IsInside(r,c)))
return 0;
else
return 1;
}
}``````

i don't exactly got your meaning. adding the braces {} you added is not necessary here i think, as the function does exactly the same without them, and well, it works - it prints the index of the cell which is surrounded only by cells with lesser numeric values

if you refer that the code can be done better i can't agree more
but as for my current level, it's the max i got 4 you, folks :)

> adding the braces {} you added is not necessary here i think,
Syntactically, you're probably right.

But I've seen WAY too many screw-ups caused by sloppy indentation and poor use of braces to bother arguing about it. Any decent code editor will add them for you, so it's really a moot point IMO.

But if you want to write code that causes you to Hunt_the_Wumpus every once in a while, then don't blame me.
If adding them is nearly free, can you justify spending perhaps days tracking down an obscure bug?

I dunno, just like the one you posted at the start of this thread perhaps....
I mean, you obviously stared at it for a while, then posted it on a forum, then waited for replies.
Imagine how a consistent approach to coding would have prevented all that nonsense.

> as the function does exactly the same without them, and well
So take out the two for loops then, and just have

``````int check_mat (int mat[N][M], int i, int j)
{
int r = 0, c = 0;
if(!(mat[i][j]>mat[r][c]&&IsInside(r,c)))
return 0;
else
return 1;
}``````

Does it still make sense to you?

...But I've seen WAY too many screw-ups caused by sloppy indentation and poor use of braces...

that's why i never use them, braces
till is absolutely necessary
i hate to fight my way thru a forest of {{{{{{{}}}}}}}
and if i put to many of them i always got errors at my compilation
as i got them messed up.

...So take out the two for loops...

damn, you're right!!! much better that way, 10x!

one last word on the function and this thread. a got help from a friend who made it perfect
taking out the loops was nice, but on second consideration if you want to check all the matrix and not only the specific case i listed
(i.e.) for cells with higher values we still need them.
and abhimanipal was right about adding = , so thank you both!

``````int check_mat (int mat[N][M], int i, int j)
{
int r;
int c;
for(r=i-1; r<=i+1 ; r++)
{
for(c=j-1; c<=j+1 ; c++)
if (IsInside(r,c) && !(r==i&&c==j)
if(mat[i][j] <= mat[r]c])
return 0;
}
return 1;
}``````

that's why i never use them, braces
till is absolutely necessary
i hate to fight my way thru a forest of {{{{{{{}}}}}}}
and if i put to many of them i always got errors at my compilation
as i got them messed up.

So sad. You have bad formatting practices and blame the braces for errors rather than learn how to format well and understand how readable and maintainable your code could be. To each his own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.