My professor is gay, i wrote this program that can test magic square and he told me i will get a zero because i am not using the so call "array and looping" as my advantage ?(which i already did,).
So basically the main problem is he is dismay by my cumbersome huge "if" statement, does anyone know how to simplify it, because i honestly don't know.
}
int a = matrix[0][0];//11
int b = matrix[0][1];//12
int c = matrix[0][2];//13
int d = matrix[1][0];//21
int e = matrix[1][1];//22
int f = matrix[1][2];//23
int g = matrix[2][0];//31
int h = matrix[2][1];//32
int i = matrix[2][2];//33
if (a, b, c, d, e, f, g, h, i >= 1 && a <= 9) {
if ((a + b + c == d + e + f)&& (d + e + f == g + h + i)&& (a + d + g == b + e + h)&& (b + e + h== c + f + i) && (a + e + i == g + e + c)&& (a!=b!=c!=d!=e!=f!=g!=h!=i))
cout <<" this is a magic square"<<endl;
else
cout <<" this is not a magic square"<<endl;
} else {
cout<<"this is not a magic square";
// bad
}
does anyone know how to simplify it, because i honestly don't know.
Yes, by getting rid of the a-i variables and operating on matrix directly by using loops.
Most of these if conditions are wrong too, because the comma operator does not do what you think it does. Writing this correctly would result in much more code. See here under "comma operator": http://www.cplusplus.com/doc/tutorial/operators/
So for example, the first if statement is equivalent to if (i>=1 && a<=9), which is probably not what you want.
A similar problem is with your use of !=. != compares two values and evaluates to a boolean value (which is either 1 or 0 when promoted to int), which you then compare with the next variable. That's certainly not what you intended.
OK, so what i need to do, for the first problem which is the "if (a, b, c, d, e, f, g, h, i >= 1 && a <= 9)" <-- i can separate each a b c d e. what about the boolean problem, how am i suppose to fix it?
A series of loops is what you'd need. Why? Look at what you need to determine:
1. Find the goal sum -- easily done by finding a horizontal, vertical or diagonal sum
2. Test horizontal sums -- there are S horizontal sums in a square, where S is the length of a side
3. Test vertical sums -- there are S vertical sums in a square, where S is the length of a side
4. Test diagonal sums -- there are 2 diagonal sums in any given square with sides of length S
I have a feeling that you might want to just use the form matrix[y][x] rather than individual letters a-z. What if your magic square became a 4x4 or even 20x20? You would be adding a LOT of variables, not to mention the problem with an even longer if statement.
My recommendation: use loops more to your advantage via matrix[y][x].
1 2 3 4 5 6 7 8 9 10
Const Integer SIDE = 3
Integer Array matrix[SIDE, SIDE] #An array of integers named `matrix' of size SIDE by SIDE
# Fill `matrix' with values -- either hard-coded, randomly generated or input by a user.
# Determine the first sum since all sums must match for the matrix to qualify as a magic square.
# Test whether all other sums match the first sum.
# Declare whether `matrix' is actually a magic square or not.