Thread: Diagonals

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Diagonals

    supose theres an array SIZE X SIZE i getin coordinate a,b from user and i want to check all the diagonals including the cell a,b in the array .

    those for loops alright ?

    1.

    for(i=a-1, j=b+1; (0<=i)||(j<SIZE-b); i--,j++)


    2.

    for(i=a+1,j=b-1; (i<SIZE-b)||(0<=j); i++,j--)


    3.

    for(i=a-1,j=b-1; ((0<=i)||(0<=j)); i--, j--)

    4.

    for(i=a+1,j=b+1; ((i<SIZE)&&(j<SIZE)); i++, j++)


    should i change || to && somewhere ?


    TNX ...........

    P.S : of course without check the cell a,b .
    Last edited by the_contractor; 11-26-2009 at 04:55 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by the_contractor View Post
    those for loops alright ?
    Probably you will need to compile them as a program and run that on a computer :P

    P.S : of course without check the cell a,b .
    Check??!? How??
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    here it is......

    Code:
    #include <stdio.h>
    #define SIZE 8
    void print(int board[SIZE][SIZE])
    {
    	int i,j;
    	for(i=0; i<SIZE; i++)
    	{
    		for(j=0; j<SIZE; j++)
    			printf("%2d",board[i][j]);
    		printf("\n");
    	}
    	printf("\n\n\n");
    }
    
    
    
    
    
    void main()
    {
    	int arr[SIZE][SIZE]={0}, i,j,a,b;
    	printf("Enter Coordinate (a,b): ");
    	scanf("%d,%d",&a,&b);
    	printf("Enter Value arr[%d][%d]: ",a,b);
    	scanf("%d",&arr[a][b]);
    
    
    	for(i=a-1, j=b+1; (0<=i)||(j<SIZE-b); i--,j++)
    	{
    		arr[i][j]=8;
    		getchar();
    		print(arr);
    	}
    	for(i=a+1,j=b-1; (i<SIZE-b)||(0<=j); i++,j--) 
    	{
    		arr[i][j]=8;
    		getchar();
    		print(arr);
    	}
    	for(i=a-1,j=b-1; ((0<=i)||(0<=j)); i--, j--)
    	{
    		arr[i][j]=8;
    		getchar();
    		print(arr);
    	}
    	for(i=a+1,j=b+1; ((i<SIZE)&&(j<SIZE)); i++, j++)
    	{
    		arr[i][j]=8;
    		getchar();
    		print(arr);
    	}
    
    }
    ive get some problems i dont know why .

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    24
    Quote Originally Posted by MK27 View Post
    Probably you will need to compile them as a program and run that on a computer :P


    Check??!? How??

    mean to skip the cell a,b .

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by the_contractor View Post
    ive get some problems i dont know why .
    I do: you are not communicating effectively. You need to describe the problems.

    This is not just to help make things easier for someone who may want to help you (but that is probably a good enough reason anyway) -- describing the problem will also help you understand it, I promise.

    80-90% of the questions I intend to ask on the forum never get posted, because as I am typing in the message box I think, well, the first thing someone is going to say is "Did you check ....?" so then I do that. Or else just finding the words makes it clearer and the answer comes along with that. Honestly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I am guessing part of your problem stems from this:
    STDIN pitfalls
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    well you can check the surrounding array elements for every point in a 2d array using three nested loops,

    outerloop, y-axis, next loop, x-axis, inner loop to check surrounding points, runs 9 times, once for each point touching your a,b and including the a,b.
    it tests, "is point y-1, x-1 my condition?, next loop, is y-1, x my condition?, next, is y-1, x+1 my condition?
    etc etc

    you need to watch out you are not searching into invalid array area though, you should have some kind of edge detection or trap for non valid array indexes.

    80-90% of the questions I intend to ask on the forum never get posted, because as I am typing in the message box I think, well, the first thing someone is going to say is "Did you check ....?" so then I do that. Or else just finding the words makes it clearer and the answer comes along with that. Honestly.
    thats so true...

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a bad design to use while you're coding up the program and testing it. Give all the 64 squares a set value, so you don't have to spend time laboriously entering data with every test you run.

    Then you should have 4 functions: rowCheck, colCheck, and diagCheck. row is very easy, column is more difficult, but just remember to increment by 8 (or whatever width you ultimately use for the board - more on that, later).

    The diagonal check function is the most difficult, but just take it one direction at a time: i like to work clockwise from straight up, so my first diagonal would be the one toward 2 o'clock. With this diagonal, you'll be subtracting 7 columns and 1 row, with each trip through the loop.

    What you have to watch out for with diagonals is "wrap around" onto the other side of the board. Always have your display function up and running, and use it to check your results, as you go.

    I sometimes use a 10 X 10 board, because that lets me put an edge value, in all the squares surrounding the center 8 by 8 board. That can be used to stop wrap around really easily.

    If you can put your problem into words, I can show you how to fix it. Just saying "it doesn't work or it has problems", doesn't work for me, because it doesn't tell me WHAT problems you're having.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. diagonals
    By the_contractor in forum C Programming
    Replies: 1
    Last Post: 11-23-2009, 03:35 AM
  2. Nested For Loops
    By smitsky in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2004, 01:58 PM