Skip to content

Instantly share code, notes, and snippets.

@manucorporat
Created February 20, 2014 17:11
Show Gist options
  • Save manucorporat/9118639 to your computer and use it in GitHub Desktop.
Save manucorporat/9118639 to your computer and use it in GitHub Desktop.
#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