1. ## magic square whoes

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]);

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!!!

2. I need a little more information... is the number that the user enters the size of the sides of the square, or is it a seed to populate a 15x15 magic square?

In other words, is your square always going to be 15x15 or is that only the MAX size of the square?

Also... what is the algorithm you are using? There are so many different ones for magic squares.

3. DELETED! Obviously magic squares != normal squares...

4. I give this magic square program frequently in my C classes. There are different variations on similar algorithms. Basically there is one method to calculate odd number magic squares (sounds like what you are doing) and "even number" magic squares. However, you cannot create magic squares for all numbers.

As for your code- I feel sorry so here is algorithm I show my students

Code:
```calculateMagic
Set row = 0
Set col = size ¸ 2
Set matrix to 1
Loop (next = 2 to size * size)
Decrement row by 1
Increment col by 1
If (row less than 0 and col greater than size – 1)
Set row = 1
Set col = size – 1
else if (row less than zero)
Set row = size – 1
else if (col greater than size – 1)
Set col = 0
else if (matrix not equal to 0)
Increment row by 2
Decrement col by 1
matrix = next
EndLoop
END```
It is all there- now put it all together.!!!

5. ## still having problems

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)
{

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;
}

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)
{
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])
{

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);

}```

6. Here's a clue on the second prog:

>>print_square(row, col, entry, magic);
What value are row, col, and entry holding when you issue this call in main() ?

7. ## 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])
{

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)
{
scanf ("%i",&entry);
printf ("\n\n");
}

/* function call */

build_square (entry, magic);

print_square (row, col, entry, magic);

value (entry, magic);

}```

8. >>to me it all seems right and I know its not because it doesnt work.
Can you be more specific than "it doesnt work" ?

9. ## sure thing

When I input the number to make the magic square, for instance in this case I use 3 as an example. The magic square does not build, instead I get an output of

Code:
```897
897
897```
The frustrating thing is I did the entire program without the use of functions and it works. When I divide it up into the functions. first one being "build the square", second "print the square" and third "calculate the magic number" it does not work.

I am sure I am just missing something that is not being passed corrrectly but I just cant seem to find it at this point. When I go through it, it seems that I am passing everything correctly.

10. Ok what is your problem now?

More clues;

Enter size: size

function prototype for magic square

void magic (int square[][MAX], int size);

now if you can read in write out a 2D array with the code provide earlier then you should have. it.