/* squareenum.c - Given a square matrix, we enumerate its content
	starting at the top left coner and then diagonally. For example
	1, 2, 3
	4, 5, 6
	7, 8, 9
	is enumerated 1, 2, 4, 3, 5, 7, 8, 9
*/

#include <stdio.h>

void enumsquare(int n, int a[n][n]) {
    int z, x, y; // We will always have x+y==z
    /* In the example above, this loop prints 
		1,2,3
		4,5
		7
    */
    for (z = 0; z < n; z++) {
	for (x = z, y = 0; x >= 0; x--, y++)
	    printf("\t%d\n", a[y][x]); 
    }
    /* In the example above, this loop prints
		  6
		8,9
    */
    for (z = n; z < n+n; z++) {
	for (x = n-1, y = z-x; y < n; x--, y++)
	    printf("\t%d\n", a[y][x]); 
    }
}

int main() {
    int a[4][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, 
			{13,14,15,16}};
    enumsquare(4, a);
    return 0;
}
