Thread: let hand diagonal traversal of a matrix

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    3

    Question let hand diagonal traversal of a matrix

    Hi, I'm trying to get an array of n*n to sum up m values in the matrix going left diagonally

    so for n = 3 and m = 2 i should only hit elements
    [0][2]
    [1][1]

    [1][1]
    [2][0]

    [1][2]
    [2][1]

    however the way i have coded it seems to be going in to negative, non existent elements. i have fixed the code to travers the matrix horizontal, vertically and right diagonally, but cant seem to figure out where I'm

    Code:
    printf("\n\n ***** Diagonal left traversal ***** \n\n") ;
          for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
    
              if((j-m) < (n+1) && (i+m) < (n+1)){
    
              y = 0 ;
    
              printf("\n\n ***** Diagonal left traversal ***** \n\n") ;
                      for (x = j; x < (j + m)  ; x++) {
    
                         tempSum = tempSum + pMatrix[i+y][j-y];
                         printf("\n array location pMatrix[%d][%d]  \n", i+y  , j-y);
    
                         y++ ;
                       }
               }
            }
        }
    Last edited by Salem; 03-14-2016 at 12:54 AM. Reason: removed colour/font abuse in code

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    I can't understand your example or your code.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    3
    no one can ever understand my code. the idea is that if i am at element [0][0] i should not be bumble to do anything. once there is enough space to access m elements diagonal then it should move from [0][2] down one row and one element behind so [1][1] and if m where three it would then move on to [2][0] to it would traverse the matrix diagonal( as in [0][0] [1][1] [2][2] ) only backwards

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    3
    sorry i have Figured out the issue, and its working the way it should now. could any one tell me how i delete this post, as i cant seem to find a way, and as it stands, its nether use nor ornament, being as no one can ever read my code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help! find diagonal sum of matrix
    By tshort82392 in forum C Programming
    Replies: 11
    Last Post: 11-06-2013, 06:49 PM
  2. Print the matrix in diagonal order - C program
    By saran.geevee in forum C Programming
    Replies: 2
    Last Post: 05-11-2012, 06:02 AM
  3. Sum of the diagonal numbers of a matrix
    By pedrocosta03 in forum C Programming
    Replies: 3
    Last Post: 07-14-2011, 08:48 PM
  4. Diagonal Matrix
    By mlupo in forum C Programming
    Replies: 1
    Last Post: 12-04-2002, 10:02 PM
  5. recursive matrix traversal
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 11:24 PM