/* sqtest.cpp - Program for testing magic squares. This program takes n^2 numbers as input and reports whether the input forms a magic square of size n. If so, it reports the smallest (start) value, step size, and the total of each row, column and diagonal (which should be the same). For example, if the input is: 100 240 230 130 210 150 160 180 170 190 200 140 220 120 110 250 Then the program reports n=4, sum=700, start=100, step=10. The program does not prompt for any input. There should be no other input except the list of numbers separated by white space. */ #include #include #include #include #include using namespace std; int main() { // Read the square vector sq; int x; while (cin >> x) sq.push_back(x); // Test whether the input size is a perfect square int n = int(sqrt(int(sq.size())) + 0.5); // round if (n < 1) { cout << "No input\n"; exit(1); } if (n*n == int(sq.size())) cout << "n = " << n << endl; else { cout << int(sq.size()) << " elements is not a perfect square\n"; exit(1); } // Add up the top row int sum=0; for (int i=0; i 1) step = (sq[n*n-1] - start) / (n*n - 1); cout << "start = " << start << endl; cout << "step = " << step << endl; // Verify elements form a sequence for (int i=0; i