I have this question where I have 10*10 array made of 0 & 1. I need to check if the 1 I find in matrix stand in the folowing conditions:
No other 1 around it nor above it or below, left, right or diagonal.
wrote the following code, but I need to fix 2 things:
1. not using goto().
2. put in index array the indexes of the 1 I find & stand the conditions.
How would you recomend me alter this code?
TIA
Code:#include<stdio.h>
#define N 10
int check(int matrix1[][N+2], int index[][2]);
main()
{
int matrix[N][N]={{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}} ;
int i,j;
int bigmatrix[N+2][N+2], index[N][2]={0};
for (i=0;i<N;i++)
for (j=0;j<N;j++)
{
bigmatrix[i+1][j+1]=matrix[i][j];
}
check (bigmatrix,index);
for (i=0;i<N;i++)
for (j=0;j<2;j++)
printf ("%d\n", index[i][j]);
getch();
}
int check(int matrix1[][N+2],int index[][2])
{
int i,j,r,k,flag=1;
for (i=1;i<N;i++)
for (j=1;j<N;j++)
while (matrix1[i][j]==1)
{
if (matrix1[i-1][j] || matrix1[i][j-1]|| matrix1[i+1][j] || matrix1[i][j+1] ||
matrix1[i-1][j-1] || matrix1[i+1][j+1] || matrix1[i-1][j+1] || matrix1[i+1][j-1])
flag=0;
for (r=i+1;r<N;r++)
if (matrix1[r][j]!=0)
flag=0;
goto label ;
for (k=j+1;k<N;k++)
if (matrix1[i][k]!=0)
flag=0;
goto label ;
for (k=j+1,r=i+1;k<N||r<N;r++,k++)
if (matrix1[r][k]!=0)
flag=0;
goto label ;
for (k=j-1,r=i-1;k>0||r<N;r++,k--)
if (matrix1[r][k]!=0)
flag=0;
goto label ;
return flag;
}
label:
if (flag==1)
printf("i=%d, j=%d\n", i-1,j-1);
else
puts ("no match found");
return flag;
}