-1

can anyone help me with this? i can get it to run but when i run it, the matrix doesnt execute correctly...i am writing it in "C"..this should be a 4x4 magic square...thanks~

#include <stdio.h>
#define R 4
#define C 4
 
void Same(int []);
int main()
{
int array[R][C], i, j, n[( R + R + 2)], out;
printf("\n * MAGIC * SQUARE *");
printf("\n\n\nThis program will ask for 16 numbers and it will store it in a matrix.");
printf("\nA magic square is an n by n matrix that is filled with numbers. The sum of");
printf("\neach row, column, and both diagonals have the same values.");
printf("\n\n\nPlease enter 16 integers below. Enter -1 to EXIT.\n");
for (i = 0; i < R; i++)
for (j = 0; j < C; j++);
{
scanf("%d", &array[i][j]);
if (array[i][j] == -1)
return 0;
}
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
printf("%d ", array[i][j]);
putchar("\n");
}
for (i = 0; i < R; i++)
{
out = 0;
for (j = 0; j < C; j++)
{
out = out + array[i][j];
}
n[i] = out;
}
for (j = 0; j < C; j++)
{
out = 0;
for (i = 0; i < C; i++)
{
out = out + array[i][j];
}
n[(j + R)] = out;
}
for (i = 0, out = 0; i < R; i++)
{
out = out + array[i][i];
}
n[( R + R )] = out;
for (i = 0, out = 0; i < R; i++ )
{
out = out + array[i][R - 1 - 1];
}
n[R + R + 1] = out;
Same(n);
return 0;
}
void Same(int total[])
{
int g = total[0], i;
for (i = 1; i < (R + R + 20; i++)
{
if (g != total[i] )
i = (R + R + 2);
}
if (i == (R + R + 3) )
printf("Not a Magic Square!\n\n");
else
printf("A Magic Square!\n\n");
}
3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by kkx123
0

can anyone help me with this? i can get it to run but when i run it, the matrix doesnt execute correctly...i am writing it in "C"..this should be a 4x4 magic square...thanks~

#include <stdio.h>
#define R 4
#define C 4
 
void Same(int []);
int main()
{
int array[R][C], i, j, n[( R + R + 2)], out;
printf("\n * MAGIC * SQUARE *");
printf("\n\n\nThis program will ask for 16 numbers and it will store it in a matrix.");
printf("\nA magic square is an n by n matrix that is filled with numbers. The sum of");
printf("\neach row, column, and both diagonals have the same values.");
printf("\n\n\nPlease enter 16 integers below. Enter -1 to EXIT.\n");
for (i = 0; i < R; i++)
for (j = 0; j < C; j++);
{
scanf("%d", &array[i][j]);
if (array[i][j] == -1)
return 0;
}
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
printf("%d ", array[i][j]);
putchar("\n");
}
for (i = 0; i < R; i++)
{
out = 0;
for (j = 0; j < C; j++)
{
out = out + array[i][j];
}
n[i] = out;
}
for (j = 0; j < C; j++)
{
out = 0;
for (i = 0; i < C; i++)
{
out = out + array[i][j];
}
n[(j + R)] = out;
}
for (i = 0, out = 0; i < R; i++)
{
out = out + array[i][i];
}
n[( R + R )] = out;
for (i = 0, out = 0; i < R; i++ )
{
out = out + array[i][R - 1 - 1];
}
n[R + R + 1] = out;
Same(n);
return 0;
}
void Same(int total[])
{
int g = total[0], i;
for (i = 1; i < (R + R + 20; i++)
{
if (g != total[i] )
i = (R + R + 2);
}
if (i == (R + R + 3) )
printf("Not a Magic Square!\n\n");
else
printf("A Magic Square!\n\n");
}

the very first error I see is a ZOMBIE ; at the end of the second for loop.

for (i = 0; i < R; i++)
for (j = 0; j < C; j++);

0

ah yea thanks guys for looking at it...
yea i figured out that there was a zombie ; at the end of the for loop ^_^ lol i was thinking too much about how the program should look like that i didnt realize that tiny little ";"!! i feel so mad at myself, hehe thanks alot guys

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.