1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
#include <iostream> using namespace std; int magicSquare[3][3]; int row; int col; int rowSum; int colSum; int main() { int rowSum1 = 0, rowSum2 = 0, rowSum3 = 0; for(int row = 0; row < 3; row++) { rowSum1 += magicSquare[0][row]; rowSum2 += magicSquare[1][row]; rowSum3 += magicSquare[2][row]; } rowSum = rowSum1 + rowSum2 + rowSum3; int colSum1 = 0, colSum2 = 0, colSum3 = 0; for(int col = 0; col < 3; col++) { colSum1 += magicSquare[0][col]; colSum2 += magicSquare[1][col]; colSum3 += magicSquare[2][col]; } colSum = colSum1 + colSum2 + colSum3; while (colSum == rowSum) { for (row = 0; row < 3; row++) { for (col = 0; col < 3; col++) { magicSquare[row][col] = rand()%9; cout << magicSquare[row][col] << " "; } cout << endl; } } return 0; }