matrix question

This is a discussion on matrix question within the C Programming forums, part of the General Programming Boards category; I have this question where I have 10*10 array made of 0 & 1. I need to check if the ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    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. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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
    #define BAD(x,y,z) \
        (\
            (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] );
                    printf("Check: %d, is %svalid.\n", BAD(y,x,matrix), BAD(y,x,matrix?"in":"" );
                }
    
        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.
    Hope is the first step on the road to disappointment.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    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. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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...

    Quote 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.
    Hope is the first step on the road to disappointment.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    goto label ;
    tsk, tsk, tsk.

  9. #9
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Sounds like the Game of Life.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sand_man
    Code:
    goto label ;
    tsk, tsk, tsk.
    Someone wasn't paying attention...

    Quote 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.
    Hope is the first step on the road to disappointment.

  11. #11
    ---
    Join Date
    May 2004
    Posts
    1,379
    i thought he meant goto() as some kind of function.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 02:14 PM
  2. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  3. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  4. Question about view vector, position, and matrix speed
    By Silvercord in forum Game Programming
    Replies: 1
    Last Post: 02-03-2003, 12:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21