1. ## matrix question

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;
}```

2. Write a function that, given some coordinates, will return false or true, depending on the surrounding squares. Pass it the array and the coordinates to check. Do boundry checking to make sure you don't wander out of bounds.

For your "index", create an array to store the resulting coordinates. You can do this simply by multiplying either the X or Y coordinate by 10, and then adding the other coordinate to it, to give you a single-value coordinate.

That should suffice for now.

Quzah.

3. This might not be the most efficient way to check, but it works:
Code:
```itsme:~/C\$ cat coords.c
#include <stdio.h>

#define N 10

int is_isolated(int matrix[N][N], int i, int j)
{
int i2, j2;

// Check up
for(i2 = i-1;i2 >= 0;--i2)
if(matrix[i2][j])
return 0;
// Check down
for(i2 = i+1;i2 < N;++i2)
if(matrix[i2][j])
return 0;
// Check left
for(j2 = j-1;j2 >= 0;--j2)
if(matrix[i][j2])
return 0;
// Check right
for(j2 = j+1;j2 < N;++j2)
if(matrix[i][j2])
return 0;
// Check up-left
for(i2 = i-1, j2 = j-1;i2 >= 0 && j2 >= 0;--i2, --j2)
if(matrix[i2][j2])
return 0;
// Check up-right
for(i2 = i-1, j2 = j+1;i2 >= 0 && j2 < N;--i2, ++j2)
if(matrix[i2][j2])
return 0;
// Check down-left
for(i2 = i+1, j2 = j-1;i2 < N && j2 >= 0;++i2, --j2)
if(matrix[i2][j2])
return 0;
// Check down-right
for(i2 = i+1, j2 = j+1;i2 < N && j2 < N;++i2, ++j2)
if(matrix[i2][j2])
return 0;

return 1;
}

int main(void)
{
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}};
struct { int i; int j; } index[N*N];
int nindex = 0;
int i, j;

for(i = 0;i < N;++i)
for(j = 0;j < N;++j)
if(matrix[i][j] && is_isolated(matrix, i, j))
{
// Store coords with top-left corner of matrix being 1,1 instead of 0,0
index[nindex].i = i+1;
index[nindex].j = j+1;
nindex++;
}

printf("Found %d isolated 1s\n", nindex);
printf("Isolated 1s can be found at:\n");
for(i = 0;i < nindex;++i)
printf("\t%d,%d\n", index[i].i, index[i].j);

return 0;
}
itsme:~/C\$ ./coords
Found 2 isolated 1s
Isolated 1s can be found at:
3,6
5,2
itsme:~/C\$```

4. Ahh yeah, quzah has a good idea there for the index. Here's the simpler, more memory-efficient main() function using quzah's suggestion:
Code:
```int main(void)
{
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 index[N*N];
int nindex = 0;
int i, j;

for(i = 0;i < N;++i)
for(j = 0;j < N;++j)
if(matrix[i][j] && is_isolated(matrix, i, j))
index[nindex++] = i+(j*10);

printf("Found %d isolated 1s\n", nindex);
printf("Isolated 1s can be found at:\n");
for(i = 0;i < nindex;++i)
printf("\t%d,%d\n", (index[i]%10)+1, (index[i]/10)+1);

return 0;
}```
And it still outputs the same thing:
itsme:~/C\$ ./coords
Found 2 isolated 1s
Isolated 1s can be found at:
3,6
5,2
itsme:~/C\$

5. Well sheesh, if you're going to do it for them...
Code:
```#include <stdio.h>
#define XSIZE 10
#define YSIZE 10
#define O(y,x) ((x)>-1&&(x)<(XSIZE)&&(y)>-1&&(y)<(YSIZE))
#define y__ y-1
#define _y_ y
#define __y y+1
#define __x x+1
#define _x_ x
#define x__ x-1
(\
(O(_y_,x__)?z[_y_][x__]:0)+\
(O(__y,x__)?z[__y][x__]:0)+\
(O(__y,_x_)?z[__y][_x_]:0)+\
(O(__y,__x)?z[__y][__x]:0)+\
(O(_y_,__x)?z[_y_][__x]:0)+\
(O(y__,__x)?z[y__][__x]:0)+\
(O(y__,_x_)?z[y__][_x_]:0)+\
(O(y__,x__)?z[y__][x__]:0) \
)

int main ( void )
{
int matrix[YSIZE][XSIZE]={
{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,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,1,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0}
};
int x, y;

/* draw it */
for( y = 0; y < YSIZE; y++ )
{
for( x = 0; x < XSIZE; x++ )
printf("%d", matrix[y][x] );
printf("\n");
}
printf("Enter to Continue...\n");
getchar( );

/* results */
for( y = 0; y < YSIZE; y++ )
for( x = 0; x < XSIZE; x++ )
if( matrix[y][x] )
{
printf("On row %d, cell %d, encountered %d.\n", y, x, matrix[y][x] );
}

return 0;
}

/*

0100000000
0000000000
0000010000
0000000000
0100000000
0000000000
0000000000
0010000000
0100000000
1000000000
Enter to Continue...

On row 0, cell 1, encountered 1.
Check: 0, is valid.
On row 2, cell 5, encountered 1.
Check: 0, is valid.
On row 4, cell 1, encountered 1.
Check: 0, is valid.
On row 7, cell 2, encountered 1.
Check: 1, is invalid.
On row 8, cell 1, encountered 1.
Check: 2, is invalid.
On row 9, cell 0, encountered 1.
Check: 1, is invalid.
*/```
Nice and simple. (I was going to make it a nifty picture, but you'll have to settle for this instead.) And yes, I have a function instead of a macro for you who are picky about macros. But macros are funner.

Quzah.

6. I couldn't really tell if the condition was that there couldn't be another 1 anywhere on one of the 6 extending lines from the 1 being checked or if there just couldn't be another 1 adjacent to the 1 being checked.

7. Originally Posted by itsme86
I couldn't really tell if the condition was that there couldn't be another 1 anywhere on one of the 6 extending lines from the 1 being checked or if there just couldn't be another 1 adjacent to the 1 being checked.
Hm...

Originally Posted by ronenk
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.
I thought they just meant in the eight surrounding squares. I guess it helps if the person asking the question is clear as to exactly what they mean.

Quzah.

8. Code:
`goto label ;`
tsk, tsk, tsk.

9. Sounds like the Game of Life.

10. Originally Posted by sand_man
Code:
`goto label ;`
tsk, tsk, tsk.
Someone wasn't paying attention...

Originally Posted by ronenk
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?

Quzah.

11. i thought he meant goto() as some kind of function.

12. Originally Posted by sand_man
i thought he meant goto() as some kind of function.
You can't have a function called "goto", because "goto" is a reserved keyword. Just like you can't make a function called "struct" or "int" or ...

Quzah.