简体   繁体   中英

Magic Square 3x3 c++

our teacher told us to make a c++ program that generates a magic square 3x3 or higher and this is what i've gotten so far but its not working. help me please.

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

using namespace std;
// A  B  C
// D  E  F
// G  H  I
int main()
{
srand(time(0));
int A,B,C,D,E,F,G,H,I;
do
{A=rand()%9+1;
B=rand()%9+1;
C=rand()%9+1;
D=rand()%9+1;
E=rand()%9+1;
F=rand()%9+1;
G=rand()%9+1;
H=rand()%9+1;
I=rand()%9+1;}
while ((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) && (C+E+G!=15) && (A!=B!=C!=D!=E!=F!=G!=H!=I));
      cout<<A<<" "<<B<<" "<<C<<endl;
      cout<<D<<" "<<E<<" "<<F<<endl;
      cout<<G<<" "<<H<<" "<<I<<endl;
return 0;
}

When you write A!=B!=C!=D you should likely write A!=B && B!=C && C!=D , or perhaps !(A==B || B==C || C==D) . The way you write it, the first comparison will result in an bool, which will then be compared to the next value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM