Print the matrix in diagonal order - C program

This is a discussion on Print the matrix in diagonal order - C program within the C Programming forums, part of the General Programming Boards category; Hi i need to write c program to print the matrix elements in diagonal order, for example, int mat[3][4] = ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Bangalore
    Posts
    2

    Print the matrix in diagonal order - C program

    Hi
    i need to write c program to print the matrix elements in diagonal order,

    for example,

    int mat[3][4] = [ 0 1 2 3
    4 5 6 7
    8 9 10 11 ]

    output print should be : 0 4 1 8 5 2 9 6 3 10 7 11

    Pl check the attachment for more clarity.
    Attached Images Attached Images  

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The way to solve a lot of problems like this, is to do it by hand, away from the computer, first. Note the row and column indices as you do it. Repeat it a few times, and you'll start to see the pattern. That pattern is your logic, and serves as the backbone of your pseudo-code, once you put it down into small steps.

    All the diagonal lines follow the same logic. The only difference is where the starting point is. Find the logic to solve that, and the rest is all a "matter of technique" as they say.

    Give that a try. We don't provide answers to questions like this - it's up to you to make that initial effort, and ask specific questions about what has you stuck. Just a general "it doesn't work", doesn't work here very well. (just a heads up).

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Bangalore
    Posts
    2
    Hi Adak,
    Thank you very much for the quick reply. i was not expecting a direct c program or pseudo-code solution for the problem. i was strucked to similar logic by all my hand trials, so i was expecting a new approch/view from others. That works, your quoate "All the diagonal lines follow the same logic. The only difference is where the starting point is" is what the core solution. i wrote a function which does the work expected ( just a paper work, i need to check it with m/c ).
    Code:
    void prtMatrixDiag(){
           int i,j,a,b,reach = 0;
           for(i = 0; i < colomn; i++ ) {
                for(j = 0; j < row; j++) {
                      a = i;
                      if(reach) 
                            b = row+1;
                      else
                             b = j;
                       while(a >= 0 && b >=0) {
                               printf("%d ", mat[b][a]);
                               b--; a++;
                       if ((j+1) == row)
                               reach = 1;
                       }
                 }
             }
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2012, 08:30 AM
  2. Sum of the diagonal numbers of a matrix
    By pedrocosta03 in forum C Programming
    Replies: 3
    Last Post: 07-14-2011, 09:48 PM
  3. Replies: 1
    Last Post: 05-30-2010, 11:22 PM
  4. Program to print 3 numbers in ascending order
    By jadedreality in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 07:32 PM
  5. Diagonal Matrix
    By mlupo in forum C Programming
    Replies: 1
    Last Post: 12-04-2002, 10:02 PM

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