3 x 3 array magic square

I modified my program to make all rows and column to add to 15 in 3x3, that will prevent the noncontinuous problem u state earlier. However i got a new problem. I don't know why my code doesn't work after i set it all rows and column to and diagonal to add to 15.
Here's the problem in my if function.
((a + b + c == 15)&& (d + e + f == 15) && (g + h + i == 15)&& (a + d + g == 15) till this point it worked, after this point my code doesn't work && (b + e + h == 15) &&(c + f + i == 15) && (a + e + i == 15) && (g + e + c == 15)&& (a!=b!=c!=d!=e!=f!=g!=h!=i))

Some how the first column which is a + d + g == 15 worked, but after i add b + e + h == 15 and so on, not matter what magic square matrix i input my code say it's not magic square. Why?



#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int a, b, c, d, e, f, g, h, i;
int matrix[3][3] = {{8,1,6},{1,5,9},{6,7,2}};

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 == 15)&& (d + e + f == 15) && (g + h + i == 15)&& (a + d + g == 15) && (b + e + h == 15) &&(c + f + i == 15) && (a + e + i == 15) && (g + e + c == 15)&& (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;


return 0;
}
Topic archived. No new replies allowed.