+ 1

[Error] 'size' undeclared here (not in a function)

I wrote a function called magic square and Functions should not have pointers implementations. I have stuck with the line int sq[size][size], int size and this prints out the error message. My code is below: #include<stdio.h> int count=1; int print(int sq[size][size], int size){ int i,j; for(i=0;i<size;i++){ for(j=0;j<size;j++) printf("%5d",sq[i][j]); printf("\n"); } } int init(int sq[size][size], int size){ int i,j; for(i=0;i<size;i++) for(j=0;j<size;j++) sq[i][j]=0; } int Algorithm(int sq[size][size],int size,int i, int j, int count){ int row,column; if(count>=size*size){ sq[i][j]=count; } else { sq[i][j]=count; row=(i-1<0)?(size-1):i-1; column=(j-1<0)?(size-1):(j-1); if(sq[row][column]){ i=(++i)%size; } else{ i=row; j=(j-1<0)?(size-1):--j; } Algorithm(sq,size,i,j,count+1); } } int main(void){ int i,j,row,col; int size; //detect the size printf("input the size of the square: "); scanf("%d",&size); while((!(size%2))||size>9||size<5){ printf("This is not a available number, plz try again:"); scanf("%d",&size); } int sq[size][size]; for(j=0;j<size;j++){ sq[size][size]=sq[size][j]; } //initialize the data init(sq,size); i=0; j=(size-1)/2; Algorithm(sq,size,i,j,count); printf("magic square of size %d : \n\n",size); print(sq,size); printf("\n\n"); } Can someone help me to fix this without using the pointer implementation. Thanks!

1st Oct 2020, 4:38 PM
Riko Lin
Riko Lin - avatar
2 Answers
+ 3
You can eliminate the error by moving the size parameter to the left of the array parameter so that size is declared before it is used by the variable-length array declaration. Be certain to update all references that call the function, too. Here I made the changes, and it started working. Other errors surfaced after that. https://code.sololearn.com/csxnBy7UaGjL/?ref=app
1st Oct 2020, 6:18 PM
Brian
Brian - avatar
+ 1
Thank you very much!!
2nd Oct 2020, 2:30 AM
Riko Lin
Riko Lin - avatar