Skip to main content
added 68 characters in body
Source Link
Asleepace
  • 605
  • 3
  • 7

C, 216 212 186 155155 145 Bytes

Just as a function that takes a matrix as input

f(int i,int **m){for(int a=0;a<i;a++){for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0!a&&!b)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printfputs("\n");}} 

Sneaky Answer6 Bytes thanks to Conor O'Brien!

4 Bytes thanks to Zacharý!

Old Answer

main(int i){int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Instead of taking an array of arguments in the main function, it uses the length of arguments instead so the input for a 4x4 matrix would be:

enter image description here

Old Answer

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int i) { int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

C, 216 212 186 155 Bytes

Just as a function that takes a matrix as input

f(int i,int **m){for(int a=0;a<i;a++){for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Sneaky Answer

main(int i){int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Instead of taking an array of arguments in the main function, it uses the length of arguments instead so the input for a 4x4 matrix would be:

enter image description here

Old Answer

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int i) { int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

C, 216 212 186 155 145 Bytes

Just as a function that takes a matrix as input

f(int i,int **m){for(int a=0;a<i;a++){for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(!a&&!b)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}puts();}} 

6 Bytes thanks to Conor O'Brien!

4 Bytes thanks to Zacharý!

Old Answer

main(int i){int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Instead of taking an array of arguments in the main function, it uses the length of arguments instead so the input for a 4x4 matrix would be:

enter image description here

Old Answer

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int i) { int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

added 224 characters in body
Source Link
Asleepace
  • 605
  • 3
  • 7

C, 216 212 186186 155 Bytes

Just as a function that takes a matrix as input

f(int i,int **m){for(int a=0;a<i;a++){for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Sneaky Answer

main(int i){int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Instead of taking an array of arguments in the main function, it uses the length of arguments instead so the input for a 4x4 matrix would be:

enter image description here

Old Answer

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int i) { int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

C, 216 212 186 Bytes

Sneaky Answer

main(int i){int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Instead of taking an array of arguments in the main function, it uses the length of arguments instead so the input for a 4x4 matrix would be:

enter image description here

Old Answer

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int i) { int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

C, 216 212 186 155 Bytes

Just as a function that takes a matrix as input

f(int i,int **m){for(int a=0;a<i;a++){for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Sneaky Answer

main(int i){int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Instead of taking an array of arguments in the main function, it uses the length of arguments instead so the input for a 4x4 matrix would be:

enter image description here

Old Answer

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int i) { int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

added 407 characters in body
Source Link
Asleepace
  • 605
  • 3
  • 7

C, 216 212212 186 Bytes

Sneaky Answer

main(int i){int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Instead of taking an array of arguments in the main function, it uses the length of arguments instead so the input for a 4x4 matrix would be:

enter image description here

Old Answer

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int c,char**v) { int i = atoi(v[1]);  // Get matrix size{ int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

C, 216 212 Bytes

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int c,char**v) { int i = atoi(v[1]);  // Get matrix size int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

C, 216 212 186 Bytes

Sneaky Answer

main(int i){int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

Instead of taking an array of arguments in the main function, it uses the length of arguments instead so the input for a 4x4 matrix would be:

enter image description here

Old Answer

main(int c,char **v){int i=atoi(v[1]);int**m=malloc(8*i);for(int a=0;a<i;a++){m[a]=malloc(4*i);for(int b=0;b<i;b++){m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0;printf("%d ",m[a][b]);}printf("\n");}} 

If only I could get rid of of those mallocs, I know C is not a great golfing language, but whatever!

Compiled with GCC on macOS Sierra.

Ungolfed

main(int i) { int **m = malloc(8 * i); // Create n*n matrix for(int a=0; a<i; a++) { // Iterate through rows m[a] = malloc(4 * i); // Allocate rows for(int b=0; b<i; b++) { // Iteratre columns // Add a 1 to cell if its start or finish, or diagnol m[a][b]=((a+b+1==i)||(a==0&&b==0)||(a==i-1&&b==i-1))?1:0; printf("%d ",m[a][b]); // Print cell } printf("\n"); // Print row } 

Usage

enter image description here

added 4 characters in body
Source Link
Asleepace
  • 605
  • 3
  • 7
Loading
added 31 characters in body
Source Link
Asleepace
  • 605
  • 3
  • 7
Loading
Source Link
Asleepace
  • 605
  • 3
  • 7
Loading