Skip to content

Instantly share code, notes, and snippets.

@gastrodon
Last active April 12, 2019 03:14
Show Gist options
  • Save gastrodon/4c01de88232b163a3e0bf280f046bc5b to your computer and use it in GitHub Desktop.
Save gastrodon/4c01de88232b163a3e0bf280f046bc5b to your computer and use it in GitHub Desktop.
Accepts input n, outputs a grid of n surrounding n-1 ... surrounding n = 1
#include <stdio.h>
int main(int argc, char *argv[]) {
int input, lowest, radius, index, flag;
input = (int) *argv[1] - 48;
flag = 1;
radius = input - 1;
index = 0;
while(1) {
lowest = input - index;
for(int i = input; i> lowest; i--) {
printf("%d ", i);
}
for(int i = (radius * 2) - (index * 2) + 1; i > 0; i--) {
printf("%d ", lowest);
}
for(int i = lowest + 1; i <= input; i++) {
printf("%d ", i);
}
printf("\n");
index += flag;
if(index == -1) {
break;
}
if(index >= radius) {
flag = flag * -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment