Created
June 20, 2014 16:14
-
-
Save lnicola/f6e0c5d61709e0f60cf7 to your computer and use it in GitHub Desktop.
walk over a square matrix in a spiral shape
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> | |
| #include <tchar.h> | |
| int main() | |
| { | |
| int a[100][100]; | |
| FILE *f = fopen("spiral.in", "rt"); | |
| int n; | |
| fscanf(f, "%d", &n); | |
| for (int i = 0; i < n; i++) | |
| for (int j = 0; j < n; j++) | |
| fscanf(f, "%d", &a[i][j]); | |
| fclose(f); | |
| for (int k = 0; k <= n / 2; k++) | |
| { | |
| for (int j = k; j < n - k; j++) | |
| printf("%d ", a[k][j]); | |
| for (int i = k + 1; i < n - k; i++) | |
| printf("%d ", a[i][n - k - 1]); | |
| for (int j = n - k - 2; j >= k; j--) | |
| printf("%d ", a[n - k - 1][j]); | |
| for (int i = n - k - 2; i > k; i--) | |
| printf("%d ", a[i][k]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment