Created
February 20, 2014 17:11
-
-
Save manucorporat/9118639 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
void printDiagonals(int rows, int columns, int matrix[][columns]) | |
{ | |
int iterations = rows + columns - 1; | |
for(int i = 0; i < iterations; ++i) { | |
int x = (i >= columns) ? columns-1 : i; | |
int y = i-x; | |
do { | |
printf("%d ", matrix[y][x]); | |
++y; | |
x = i - y; // x+y = i | |
} while(x >= 0 && y < rows); | |
printf("\n"); | |
} | |
} | |
int main() | |
{ | |
int matrix[3][4] = | |
{ | |
{3, 4, 5, 6}, | |
{6,22,56,2}, | |
{3,4, 9, 1}, | |
}; | |
printDiagonals(3, 4, matrix); | |
} | |
/* IT PRINTS | |
3 | |
4 6 | |
5 22 3 | |
6 56 4 | |
2 9 | |
1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment