마방진 한계값 초과되서 나옵니다.
봄바람
마방진을 그리는데 최대값인 25를 초과하네요 어디가 문제쵸?질문 내용 :
#include stdio.h
#include stdlib.h
#define max_size 5
void main(void){
int square[max_size][max_size];
int i, j, row, column;
int count;
int size;
printf(enter the size of the square:);
scanf(%d, &size);
if(size1 || size max_size+1){
fprintf(stderr,error! size is out of range\n);
exit(1);
}
if(!(size %2)){
fprintf(stderr,error! size is even);
}
square[0][(size-1)/2]=1; //첫번째 행의 중앙에 1을 넣는다.
/*i와 j는 현재위치 */
i=0;
j=(size-1)/2;
for(count=2; count=size*size; count++){
row=(i-1)0?(size-1):(i-1); //위쪽으로
column=(j-1)0?(size-1):(j-1); //왼쪽으로
if(square[row][column]) /*아래로 */
i=(++i)%size;
else { //정방형이 비어 있을경우
i =row;
j=(j-10)?(size -1): --j;
}
square[i][j]=count;
}
/*정방형을 출력*/
printf(magic square of size %d:\n\n, size);
for (i=0; isize; i++){
for(j=0; jsize;j++){
printf(%5d , square[i][j]);
}
printf(\n);
}
printf(\n\n);
system(pause);
}