well,i'm working on this program that has been asked many times.
"The magic squares". here what i got so far. I can compile it with no errors, but with no output. I think i'm not passing the arrays correctly.
Any suggestions?
Code:
#include<stdio.h>
#define MAX 15
void intarray(int [][MAX],int);
void calcsquare(int[][MAX],int);
void printarray(int [][MAX],int);
int main(void)
{
int num;
int set_num[MAX][MAX];
printf("Enter the size of the magic square <15 or less>: ");
scanf("%d",&num);
intarray(set_num,num);
calcsquare(set_num,num);
printarray(set_num,num);
}
void intarray(int matrix[][MAX],int num)
{
int row,column;
//initialize the arrays//
for(row=0;row<num;row++)
for(column=0;column<num;column++)
matrix[row][column] = 0;
}
void calcsquare(int square[] [MAX],int size)
{
int next;
int row =0;
int column =size/2;
square[row][column]=1;//place a 1 on center of 1st row//
for(next=2;next<(size*size);next)
{
row--;
column++;
if(row<0 && column>size-1)
{
row=0;
column=size-1;
}
else if(row<0)
{
row=size-1;
}
else if(column>size-1)
{
column=0;
}
else if(square[row][column]!=0)
{
row =row+2;
column=column-1;
}
square[row][column]=next;//continue with the next number//
}
}
void printarray(int matrix[][MAX],int n)
{
int row,column;
for(row=0;row<n;row++)
for(column=0;column<n;column++)
printf(" %5d", matrix[row][column]);
printf("\n");
}