C Program to Print a Matrix Diagonally

  • Write a C program to print a matrix diagonally from top to bottom

Given a matrix of size m x n, we have to print the matrix diagonally from right to left and top to bottom. We have to print one diagonal in a separate line.

The minor diagonal divides a matrix into two parts, elements above minor diagonal(upper half) and elements below minor diagonal(lower half). In below mentioned C program, we first print upper half of matrix then lower half of matrix diagonally.

For example, If input matrix is 
1 2 3 4
5 6 7 8
9 0 1 2
3 4 5 6
Then we should print output like this
1
2 5
3 6 9
4 7 0 3
8 1 4
2 5
6

Algorithm to print a matrix diagonally
Let A be a matrix of dimension M x N.

  • Minor diagonal of a matrix, divides it into two sections. All the elements above diagonals are upper diagonal elements and all the elements below diagonals are lower diagonal elements. We will print the matrix diagonally in two sections, first we will print upper diagonal elements then lower diagonal.
  • To print the upper diagonal elements we will use two for loops(check line 24 and 26 of below program). Outer loop will iterate over cols whereas inner loop will move the control left-down direction(by increasing row and decreasing column)
  • To print the lower diagonal elements we will use two for loops(check line 33 and 35 of below program). Outer loop will iterate over rows whereas inner loop will move the control left-down direction(by increasing row and decreasing column)

C program to print a matrix diagonally

C Program to Print a Matrix Diagonally

/*
* C Program to print a matrix diagonally from top to bottom
*/
 
#include <stdio.h>
#include <conio.h>
 
int main(){
    int rows, cols, rowCounter, colCounter, currentRow, currentCol;
    int inputMatrix[50][50];
     
    /*  Input matrix*/
    printf("Enter size of matrix\n");
    scanf("%d %d", &rows, &cols);
     
    printf("Enter the matrix of size %dX%d\n", rows, cols);
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            scanf("%d", &inputMatrix[rowCounter][colCounter]);
        }
    }
    printf("Printing matrix diagonally\n");
    // Print Upper half of matrix
    for(colCounter = 0; colCounter < cols; colCounter++){
        currentCol = colCounter; currentRow = 0;
        for(;currentCol >= 0 && currentRow < rows; currentCol--, currentRow++){
            printf("%d ", inputMatrix[currentRow][currentCol]); 
        }
        printf("\n");
    }
     
    // Print Lower half of matrix
    for(rowCounter = 1; rowCounter < rows; rowCounter++){
        currentCol = cols -1; currentRow = rowCounter;
        for(;currentCol >= 0 && currentRow < rows; currentCol--, currentRow++){
            printf("%d ", inputMatrix[currentRow][currentCol]); 
        }
        printf("\n");
    }
     
    getch();
    return 0;
}

Program Output

Enter the size of matrix
3 3
Enter matrix of size 3X3
1 2 3
4 5 6
7 8 9
Printing matrix diagonally
1
2 4
3 5 7
6 8
9

Enter the size of matrix
2 4
Enter matrix of size 2X4
1 2 3 4
5 6 7 8
Printing matrix diagonally
1
2 5
3 6
4 7
8