#include <stdio.h>
#define N 5
int main(void) {
int i,j,dim;
int matrix[N][N];
// init and print the matrix
for (i=0; i < N; i++)
{
for (j=0; j< N; j++)
{
matrix[i][j] = i*N + j;
}
}
// perform spiral print
for (dim = 0; dim < (N+1)/2; dim++)
{
// set initial i and go till the "last colomn"
i = dim;
for (j = dim; j < N - dim; j++)
{
}
// bring back i and j to the proper coordinate
// and move down to the "last row"
j--;i++;
for (i=dim+1; i < N - dim; i++)
{
}
// bring back i and j to the proper coordinate
// and move back to the "first colomn"
i--;j--;
for (; j >= dim; j--)
{
}
// bring back i and j to the proper coordinate
// and move up to the "first row"
j++;i--;
for (; i > dim; i--)
{
}
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIE4gNQoKaW50IG1haW4odm9pZCkgewoJaW50IGksaixkaW07CglpbnQgbWF0cml4W05dW05dOwoJCgkvLyBpbml0IGFuZCBwcmludCB0aGUgbWF0cml4Cglmb3IgKGk9MDsgaSA8IE47IGkrKykKCXsKCQlmb3IgKGo9MDsgajwgTjsgaisrKQoJCXsKCQkJbWF0cml4W2ldW2pdID0gaSpOICsgajsKCQkJcHJpbnRmKCIlMmQgIiwgbWF0cml4W2ldW2pdKTsKCQl9CgkJcHJpbnRmKCJcbiIpOwoJfQoJCQoJcHJpbnRmKCJcbiIpOwoJCgkvLyBwZXJmb3JtIHNwaXJhbCBwcmludAoJZm9yIChkaW0gPSAwOyBkaW0gPCAoTisxKS8yOyBkaW0rKykKCXsKCQkvLyBzZXQgaW5pdGlhbCBpIGFuZCBnbyB0aWxsIHRoZSAibGFzdCBjb2xvbW4iCgkJaSA9IGRpbTsKCQlmb3IgKGogPSBkaW07IGogPCBOIC0gZGltOyBqKyspCgkJewoJCQlwcmludGYoIiUyZCAiLCBtYXRyaXhbaV1bal0pOwoJCX0KCQlwcmludGYoIlxuIik7CgkJCgkJLy8gYnJpbmcgYmFjayBpIGFuZCBqIHRvIHRoZSBwcm9wZXIgY29vcmRpbmF0Z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