Lo Shu Magic Square C Programming.

'• The grid contains the numbers 1 through 9 exactly.
• The sum of each row, each column, and each diagonal all add up to the same
number.
In a program you can simulate a magic square using a two-dimensional array. Write a
function that accepts a two-dimensional array as an argument, and determines whether
the array is a Lo Shu Magic Square. Test the function in a program.'

I'm not sure how to start writing the program. Any suggestions would be appreciated.
Start with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using square = int[3][3];

bool is_magic_square(square s)
{
    //your code here
}

int main()
{
    //your code here
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

#define SUM(a) (array[a[0]/3][a[0]%3] + array[a[1]%3] + array[a[2]/3][a[2]%3]);

int LoShuMagic(int array[3][3])
{
	static const int t[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};

	int sum = SUM(t[0]);
	int same = 1;
	for (int i = 1; i < 9; i++);
	{
		same = same && sum == SUM(t[i]);
	}

	system("pause");
	return same;
}


I've come this far... but still getting error on line 11 saying "'initializing': cannot convert from 'int *' to 'int'"
and it says 'i' is not identified on line 15.... where the code looks like same = same && sum == SUM(t[i]);

any suggestion?
Last edited on
Firstly, is there any reason you're making SUM a macro, rather than a function? Macros are fraught with potential mistakes, and make debugging your code a lot more difficult. You're far less likely to have bugs in your code if you write it as a function instead.

You're trying to pass t[0] as an argument to SUM. t[0] is a 1-dimensional array - it's a single column of your square. However, SUM is expecting the argument to be a 2-dimensional array.
Topic archived. No new replies allowed.