Jan 312013
 

Given a square matrix of order N*N, write code to print all the elements in the order of their diagonal. For example, in the below matrix, the elements should be printed in the marked (in red) order, and the final output should be as shown below:

Solution:

We did a similar question yesterday, that was to print two diagonals of the matrix. The code to print all the elements will be similar. Basically we will be printing one small diagonal at a time.

There will be total 2*n-1 such diagonals (each arrow represents one diagonal in the above diagram).

Code:

#define N 4
void printAllDgl(int a[N][N])
 {
    int i=0, j=0;

   // Loop to print each diagonal
    for(int cnt=0; cnt<2*N-1; cnt++)
    {
       cout<<std::endl;

       if(cnt<N) {
          i = cnt;
         j = 0;
       }
       else {
          i = N-1;
         j = (cnt+1)%N;
       }

       while(i>=0 && j<N)
       {
          cout << " " << a[i][j];
          i--;
         j++;
       }
   }
}

Feel free to give your feedback and comments.
————————————————-

  4 Responses to “Print elements of a matrix in diagonal order”

Comments (3) Pingbacks (1)
  1. /*
    Author: Vishnu NK
    A program to print all diagonal elements in a matrix.
    In the given matrix output should be like:
    3->2->4->7->1->8->3->8->7->9->4->2->6->6->0->6->1->5->3->8->9->4->2->1->2

    */

    #include <iostream>
    using namespace std;

    int main(){
    int i,j,m=5,n=5;
    int mat[5][5] = { {3,4,8,9,0},
    {2,1,7,6,3},
    {7,8,6,5,4},
    {3,2,1,9,1},
    {4,6,8,2,2}
    };

    for(i=0; i<5; i++){
    for(j=0; j<5; j++){
    cout << mat[i][j] << " ";
    }
    cout << endl;
    }
    int row=0, col=0;
    int count = 0;
    while(count < (m+ (n-1))){
    //cout << count << endl;
    for(row=0; row<5; row++){
    for(col=0; col<5; col++){
    if(row+col == count){
    //cout << "Row: " << row << " :: Col: " << col << " :: Count: " << count << endl;
    cout << mat[col][row] << " ";
    }
    }
    }
    cout << endl;
    count++;
    }
    return 0;
    }

  2. public class Ajit {
    private static void diagonalPrint(int[][] m) {
    int row=m.length;
    int col = m[0].length;
    for (int i =1;i<=(row+col-1);i++){
    int start_col = max(0,i-row);
    int count = min(i, (col-start_col), row);
    for (int j=0; j<count; j++)
    System.out.print(m[min(row,i)-j-1][start_col+j]+" ");
    System.out.println();
    }
    }
    static int min(int a, int b) {
    return (a < b) ? a : b;
    }
    static int min(int a, int b, int c) {
    return min(min(a, b), c);
    }
    static int max(int a, int b) {
    return (a > b) ? a : b;
    }
    private static void print(int[][] m) {
    for (int i = 0; i < m.length; i++) {
    for (int j = 0; j < m[0].length; j++)
    System.out.print(m[i][j]+" ");
    System.out.println();
    }
    }
    public static void main(String[] args) {
    int M[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },};
    print(M);
    diagonalPrint(M);
    }
    }

  3. import java.util.Scanner;

    class Sumit
    {
    public static void main(String args[])
    {
    int n,i,j,count=1;

    Scanner in = new Scanner(System.in);
    System.out.println(“Enter the number for diagonal matrix”);
    n = in.nextInt();
    int matrix[][] = new int[n][n];
    for ( int c = 0 ; c < n ; c++ )
    for ( int d = 0 ; d < n ; d++ )
    {
    matrix[c][d] = count;
    count++;
    }

    for(int cnt=0;cnt<2*n-1;cnt++)
    {
    //System.out.println();
    if(cnt=0&&j<n)
    {
    System.out.print(" "+matrix[i][j]);
    i–;
    j++;
    }
    }
    }
    }

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)