Simplifying it.

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.


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>

using namespace std;
int a, b, c, d, e, f, g, h, I;

int matrix[3][3] = {{7,17,3},{5,9,13},{15,1,10}};

int main()
{
srand (time(0));
int number;




for(int x=0;x<3;x++)
{

for(int y=0;y<3;y++)

{//number = rand()%9+1;
// matrix[x][y]=number;
//cout<<matrix[x][y]<<"\t";
}
cout<<endl;
}


for(int x=0;x<3;x++) {
for(int y=0;y<3;y++)
{
cout<<matrix[x][y];

}
cout<<endl;

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




return 0;
}
hunkeelin wrote:
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.
Last edited on
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?
You're not supposed to separate them, you should use loops:
1
2
3
bool all_ge_one=true;
for (int j=0;j<3;j++)for (int i=0;i<3;i++)if (matrix[i][j]<1)all_ge_one=false;
if (all_ge_one && a<=9)...


Same for the comparison "chain".
I'm not gay, but I would give you, hunkeelin, 0 for the original solution, too.
As a general rule, if you find yourself declaring variables like this:

int a, b, c, d, e, f, g, h, I;

you're doing it wrong. And be consistent. What's with "I"?

Incidentally, what has your professor's sexuality got to do with anything? Do you mean to imply straight people don't care for quality programming?
to Arthar, that will be generating a magic square. The requirement for my program is to test a magic square, i may hardcode or let user to input 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. 
Last edited on
Topic archived. No new replies allowed.