// Read SIZE x SIZE numbers and test if // they form a magic square http://en.wikipedia.org/wiki/Magic_square // // and exits if the same number is read twice // // Andrew Taylor - andrewt@unsw.edu.au // 10/4/13 /* // Lo Shu Square // 4 9 2 // 3 5 7 // 8 1 6 // // Magic square of primes // 17 89 71 // 113 59 5 // 47 29 101 */ #include #define SIZE 3 int main(void) { int x[SIZE][SIZE]; int row, column; int magicConstant, sum; int notMagicSquare; // read potential magic square printf("Enter %d numbers please:\n", SIZE*SIZE); row = 0; while (row < SIZE) { column = 0; while (column < SIZE) { if (scanf("%d", &x[row][column]) != 1) { printf("Couldn't read enough numbers\n"); return 0; } column = column + 1; } row = row + 1; } printf("Numbers are:\n"); // print potential magic square row = 0; while (row < SIZE) { column = 0; while (column < SIZE) { printf("%d ", x[row][column]); column = column + 1; } printf("\n"); row = row + 1; } // sum first row magicConstant = 0; row = 0; while (row < SIZE) { magicConstant = magicConstant + x[0][row]; row = row + 1; } // check if sum of each row matches sum of first row notMagicSquare = 0; row = 1; while (row < SIZE) { sum = 0; column = 0; while (column < SIZE) { sum = sum + x[row][column]; column = column + 1; } if (sum != magicConstant) { notMagicSquare = 1; } row = row + 1; } // check if sum of each columns matches sum of first rowc column = 0; while (column < SIZE) { sum = 0; row = 0; while (row < SIZE) { sum = sum + x[row][column]; row = row + 1; } if (sum != magicConstant) { notMagicSquare = 1; } column = column + 1; } // summing diagonals left as an exercise if (notMagicSquare == 0) { printf("Is a magic square\n"); } else { printf("Not a magic square\n"); } return 0; }