Last active
November 26, 2017 11:19
-
-
Save lucarinelli/aa155dc39dc9c7438429bb77f479a489 to your computer and use it in GitHub Desktop.
LAIB 8 Exercise 1: Magic square
This file contains 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 <stdlib.h> | |
int rs[]={1,0,0,0},cs[]={0,1,0,0},rc[]={0,1,1,1},cc[]={1,0,1,-1}; | |
int magic_r(int **magic_square,int n,int row,int col,int target_sum,int *marked,int n_elements){ | |
int i,j,k,sum,under=1,wrong; | |
for(i=1;i<=n_elements&&under;i++){ | |
if(!marked[i-1]){ | |
magic_square[row][col]=i; | |
marked[i-1]=1; | |
wrong=0; | |
for(j=0;j<4;j++) { | |
sum=0; | |
for (k = 0; k < n; k++)sum += magic_square[rs[j] * row + rc[j] * k][(cc[j]<0?n-1:0) + cs[j] * col + cc[j] * k]; | |
if(sum>target_sum)under=0; | |
if(sum!=target_sum&&((j==0&&col==n-1)||(j==1&&row==n-1)||(j==2&&col==n-1&&row==n-1)||(j==3&&row==n-1)))wrong=1; | |
} | |
//printf("\n\nR%d C%d U%d W%d\n",row,col,under,wrong);for (k = 0; k < n; k++) { for (j = 0; j < n; j++)printf("%d ", magic_square[k][j]);printf("\n"); } | |
if(under&&!wrong){ | |
if(col == n - 1 && row == n - 1)return 1; | |
if(magic_r(magic_square,n,col+1<n?row:row+1,col+1<n?col+1:0,target_sum,marked,n_elements))return 1; | |
} | |
magic_square[row][col]=0; | |
marked[i-1]=0; | |
} | |
} | |
return 0; | |
} | |
int **doMagic(int n){ | |
int i,j,sum=0, **magic_square; | |
int n_elements=n*n; | |
for(i=1;i<=n_elements;i++)sum+=i; | |
int *marked=(int*)malloc(sizeof(int)*n*n); | |
if(marked==NULL)return NULL; | |
for(i=0;i<n_elements;i++)marked[i]=0; | |
magic_square=(int**)malloc(sizeof(int*)*n); | |
if(magic_square==NULL){printf("ERROR: memory allocation magic_square.\n");return NULL;} | |
for(i=0;i<n;i++){ | |
magic_square[i]=(int*)malloc(sizeof(int)*n); | |
if(magic_square[i]==NULL){printf("ERROR: memory allocation magic_square.\n");return NULL;} | |
} | |
for(i=0;i<n;i++)for(j=0;j<n;j++)magic_square[i][j]=0; | |
if(magic_r(magic_square, n, 0, 0, sum/n, marked, n_elements)){ | |
free(marked); | |
return magic_square; | |
} | |
else{ | |
free(marked); | |
for(i=0;i<n;i++)free(magic_square[i]); | |
free(magic_square); | |
return NULL; | |
} | |
} | |
int main(int argc,char **argv){ | |
FILE *output; | |
int **magic_square,n,i,j; | |
if(argc<3){ | |
printf("ERROR: Wrong number of arguments! An integer and a file name needed.\n"); | |
return EXIT_FAILURE; | |
} | |
n=atoi(argv[1]); | |
if(n==0)return EXIT_FAILURE; | |
output=fopen(argv[2],"w"); | |
if(output==NULL){ | |
printf("ERROR: Can't open %s.\n",argv[2]); | |
return EXIT_FAILURE; | |
} | |
magic_square=doMagic(n); | |
if(magic_square==NULL){ | |
printf("ERROR: Something bad happened while doing magic.\n"); | |
return EXIT_FAILURE; | |
}else{ | |
for (i = 0; i < n; i++) { | |
for (j = 0; j < n; j++)fprintf(output, "%d ", magic_square[i][j]); | |
fprintf(output, "\n"); | |
} | |
} | |
fclose(output); | |
for(i=0;i<n;i++)free(magic_square[i]); | |
free(magic_square); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment