I am trying to figure this out. I wrote the program without functions but need to have it with functions. Once I tried to put the code in functions I cant get it to work. I need to have a magic square where the user inputs an odd number between 3-15, then print the squre to the screen. I also need to add the col, row and diag.. but I worry about that later. I have been trying different things for a week now with no solution in site. this si what I have right now..
Code:
#include<stdio.h>
void build_square (int magic[15][15])
{
int row;
int col;
int base=1;
int entry;
printf("Build a Magic Square!\n\n");
printf("Enter an odd number between 3 and 15 then hit enter: ");
scanf("%d",&entry);
printf("\n\n");
row = 0; col=(entry - 1)/2; /* establishes the center square of the first row */
/* algorithm with while/if else to establish where value are in col and row */
/* based on rule given for the magic square game */
while (base <= entry*entry) {
magic[row][col] = base;
if (base == entry*(base/entry)) row++;
else {
row--; col++;
if (row < 0) row = entry - 1;
if (col > entry - 1) col = 0;
}
base++;
}
}
main()
{
int row,col,entry=15,magic[15][15], magic_number;
/* function call */
build_square (magic[row][col]);
printf("Your Magic Square!\n\n");
for(row = 0; row<entry;row++) {
for (col=0;col<entry;col++) printf("%4d",magic[row][col]);
printf("\n");
}
}
no matter what I enter I get a square of 15 with strange intergers!!!