forked from CodXCrypt/Cpp-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c94e00
commit 479c046
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define ROW 5 | ||
#define COL 4 | ||
|
||
// A utility function to find min of two integers | ||
int minu(int a, int b) | ||
{ return (a < b)? a: b; } | ||
|
||
// A utility function to find min of three integers | ||
int min(int a, int b, int c) | ||
{ return minu(minu(a, b), c);} | ||
|
||
// A utility function to find max of two integers | ||
int max(int a, int b) | ||
{ return (a > b)? a: b; } | ||
|
||
// The main function that prints given matrix in | ||
// diagonal order | ||
void diagonalOrder(int matrix[][COL]) | ||
{ | ||
// There will be ROW+COL-1 lines in the output | ||
for (int line=1; line<=(ROW + COL -1); line++) | ||
{ | ||
/* Get column index of the first element | ||
in this line of output. | ||
The index is 0 for first ROW lines and | ||
line - ROW for remaining lines */ | ||
int start_col = max(0, line-ROW); | ||
|
||
/* Get count of elements in this line. The | ||
count of elements is equal to minimum of | ||
line number, COL-start_col and ROW */ | ||
int count = min(line, (COL-start_col), ROW); | ||
|
||
/* Print elements of this line */ | ||
for (int j=0; j<count; j++) | ||
printf("%5d ", | ||
matrix[minu(ROW, line)-j-1][start_col+j]); | ||
|
||
/* Print elements of next diagonal on next line */ | ||
printf("\n"); | ||
} | ||
} | ||
|
||
// Utility function to print a matrix | ||
void printMatrix(int matrix[ROW][COL]) | ||
{ | ||
for (int i=0; i< ROW; i++) | ||
{ | ||
for (int j=0; j<COL; j++) | ||
printf("%5d ", matrix[i][j]); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
int M[ROW][COL] = {{1, 2, 3, 4}, | ||
{5, 6, 7, 8}, | ||
{9, 10, 11, 12}, | ||
{13, 14, 15, 16}, | ||
{17, 18, 19, 20}, | ||
}; | ||
printf ("Given matrix is \n"); | ||
printMatrix(M); | ||
|
||
printf ("\nDiagonal printing of matrix is \n"); | ||
diagonalOrder(M); | ||
return 0; | ||
} |