tried to use hin t to fix values passed and no luck still
I am just not seeing something obvious I am sure.. but I just cant get it... to me it all seems right and I know its not because it doesnt work.
Code:
#include<stdio.h>
#define MAX_SIZE 15 /* maximum size of square, must be odd */
/************************************************************************/
/* Function: build_square */
/* */
/* Purpose: Obtains input from user, tests if input is odd */
/* builds magic square based on input value */
/* and stores the results in an multidimensional array */
/* */
/* Parameters: magic[MAX_SIZE][MAX_SIZE] - multidimensional array */
/* */
/* */
/* Returns: Nothing, magic[MAX_SIZE][MAX_SIZE] array are passed */
/* by reference. */
/************************************************************************/
void build_square (int entry, int magic[MAX_SIZE][MAX_SIZE])
{
/* local variables */
int row, col, count;
count=0; /* sets the count for the loop */
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 */
/* countd on rule given for the magic square game */
while (count <= entry*entry)
{
magic[row][col] = count;
if (count == entry * (count/entry))
row++;
else {
row--;
col++;
if (row < 0)
row = entry - 1;
if (col > entry - 1)
col = 0;
}
count++;
}
}
/************************************************************************/
/* Function: print_square */
/* */
/* Purpose: Prints the magic square */
/* */
/* Parameters: row = row of magic square */
/* col = columns of magic square */
/* entry = number user entered */
/* magic[MAX_SIZE][MAX_SIZE] = multidimensional array */
/* */
/* Returns: Nothing */
/************************************************************************/
void print_square (int row, int col, int entry, int magic[MAX_SIZE][MAX_SIZE])
{
printf("Your Magic Square!\n\n");
for (row = 0; row < entry; row++) {
for (col = 0; col < entry; col++)
printf("%i", magic[row][col]);
printf("\n");
}
}
/************************************************************************/
/* Function: value */
/* */
/* Purpose: Finds the value of rows, col and diagnol */
/* */
/* Parameters: row = row of magic square */
/* col = columns of magic square */
/* entry = number user entered */
/* magic[MAX_SIZE][MAX_SIZE] = multidimensional array */
/* */
/* Returns: Nothing */
/************************************************************************/
void value (int entry, int magic[MAX_SIZE][MAX_SIZE])
{
int all_equal=0;
int i=0;
for (i = 0; i < entry; i++)
all_equal = all_equal + magic[0][i];
printf("\nAll rows, columns and diagonals added equal %i.\n", all_equal);
}
main()
{
int row=0, col=0, entry, remain, magic[MAX_SIZE][MAX_SIZE];
/* collects the size of the magic square */
printf("Build a Magic Square!\n\n");
printf("Enter an odd number between 3 and 15 then hit enter: ");
scanf("%i", &entry);
printf("\n\n");
/* tests for odd number */
remain = entry % 2;
if (remain == 0)
{
printf ("Your number is even, please enter an odd number: ");
scanf ("%i",&entry);
printf ("\n\n");
}
/* function call */
build_square (entry, magic);
print_square (row, col, entry, magic);
value (entry, magic);
}