diagonals

This is a discussion on diagonals within the C Programming forums, part of the General Programming Boards category; supose theres an array SIZE X SIZE the user enter coaordinate (a,b) i want to put 9 in each coardinate ...

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

    diagonals

    supose theres an array SIZE X SIZE
    the user enter coaordinate (a,b)
    i want to put 9 in each coardinate on the 2 diagonals that including (a,b).


    this is my try :

    Code:
    //////  diagonal I  {/}  ////
    
    // UP
    for(i=a-1, j=b+1; (0<=i)||(j<SIZE-b); i--,j++)
    board[i][j]=9;
    
    
    // DOWN
    for(i=a+1,j=b-1; (i<SIZE-b)||(0<=j); i++,j--)			
    board[i][j]=9;
    
    
    
    
    //////  diagonal II  {\}  ////
    
    // UP
    for(i=a-1,j=b-1; ((0<=i)||(0<=j)); i--, j--)  
    board[i][j]=9;
    
    // DOWN
    for(i=a+1,j=b+1; ((i<SIZE-1)||(j<SIZE-1)); i++, j++)
    board[i][j]=9;

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    //////  diagonal I  {/}  ////
    
    // UP-RIGHT
    for(i=a-1, j=b+1; (0<=i) && (j<SIZE); i--,j++)
       board[i][j]=9;
    
    
    // DOWN-LEFT
    for(i=a+1,j=b-1; (i<SIZE) && (0<=j); i++,j--)			
      board[i][j]=9;
    
    
    
    
    //////  diagonal II  {\}  ////
    
    // UP-LEFT
    for(i=a-1,j=b-1; ((0<=i) && (0<=j)); i--, j--)  
      board[i][j]=9;
    
    // DOWN-RIGHT
    for(i=a+1,j=b+1; ((i<SIZE) && (j<SIZE)); i++, j++)
      board[i][j]=9;
    You need AND not OR. You want both conditions to be true, to make sure none of the axis goes out of bounds.
    Don't know why you had SIZE-a or SIZE-b. You just check them to be <=SIZE-1 or <SIZE (same thing).
    Last note, you don't include (a,b). You start with i=a+1 or i=a-1 and j=b+1 or j=b-1. Never i=a or j=b. You could change that if that is what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested For Loops
    By smitsky in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2004, 12:58 PM

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