I have tried the suggestion as follows.. and cant get that to work
2nd code is my code which wont work either...I just cant figure what I am doing wrong.. when I pull it out of functions it works....
Code:
#include<stdio.h>
#define MAX_SIZE 15 /* maximum size of square, must be odd */
main()
{
int row,col,next,entry,magic[MAX_SIZE][MAX_SIZE], all_equal,remain,matrix;
/* 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("%d",&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 ("%d",&entry);
printf ("\n\n");
}
/* establishes the center square of the first row */
row = 0;
col=(entry - 1)/2;
matrix = 1;
next = 1;
/* algorithm with while/if else to establish where value are in col and row */
/* nextd on rule given for the magic square game */
while (next != entry*entry) {
row--;
col++;
if (row < 0){
if (col > entry - 1){
row = 1;
col = entry - 1;
}
}
else if (row < 0) {
row = entry - 1;
}
else if (col > entry -1){
col = 0;
}
else if (matrix != 0){
row = row + 2;
col--;
}
matrix = next;
}
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");
}
for (col = 0; col < entry; col++) all_equal += magic[0][col];
printf("\nAll rows, columns and diagonals added equal %d.\n",all_equal);
}
my other code
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 magic[MAX_SIZE][MAX_SIZE])
{
/* local variables */
int row, col, count, entry, remain;
/* 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("%d", &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 ("%d",&entry);
printf ("\n\n");
}
count=0; /* sets the count for the loop */
row = 0; col=(entry - 1)/2; /* establishes the center square of the first row */
magic[row][col] = count;
/* algorithm with while/if else to establish where value are in col and row */
/* countd on rule given for the magic square game */
do
{
if (count == entry * (count/entry))
row++;
else {
row--;
col++;
if (row < 0)
row = entry - 1;
if (col > entry - 1)
col = 0;
}
count++;
}
while (count != entry*entry);
}
/************************************************************************/
/* 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("%4d", 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 col, int entry, int all_equal, int magic[MAX_SIZE][MAX_SIZE])
{
for (col = 0; col < entry; col++) all_equal += magic[0][col];
printf("\nAll rows, columns and diagonals added equal %d.\n",all_equal);
}
main()
{
int row, col, entry, magic[MAX_SIZE][MAX_SIZE];
/* function call */
build_square (magic);
print_square(row, col, entry, magic);
value (row, col, entry, magic);
}