The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number.
Write a program that simulates a magic square using 3 one dimensional parallel arrays of integer type.
Each one the arrays corresponds to a row of the magic square.
The program asks the user to enter the values of the magic square row by row and informs the user if the grid is a magic square or not.
Use the following template to start your project:
#include<iostream>
using namespace std;
// Global constants
const int ROWS = 3; // The number of rows in the array
const int COLS = 3; // The number of columns in the array
const int MIN = 1; // The value of the smallest number
const int MAX = 9; // The value of the largest number
#include<iostream>
using namespace std;
// Global constants
const int ROWS = 3; // The number of rows in the array
const int COLS = 3; // The number of columns in the array
const int MIN = 1; // The value of the smallest number
const int MAX = 9; // The value of the largest number
int main() {
int first[COLS];
int second[COLS];
int third[COLS];
cout << "Enter first row: ";
cin >> first[0] >> first[1] >> first[2];
cout << "Enter second row: ";
cin >> second[0] >> second[1] >> second[2];
cout << "Enter third row: ";
cin >> third[0] >> third[1] >> third[2];
for (int i = 0; i < 3; i++) {
if (first[i] < MIN || first[i] > MAX ||
second[i] < MIN || second[i] > MAX ||
third[i] < MIN || third[i] > MAX) {
cout << "Incorrect input";
return 0;
}
}
int sum = first[0] + first[1] + first[2];
int horSum[ROWS], verSum[COLS], diagSum1 = 0, diagSum2 = 0;
for (int i = 0; i < ROWS; i++) {
horSum[i] = verSum[i] = 0;
}
for (int i = 0; i < COLS; i++) {
horSum[0] += first[i];
horSum[1] += second[i];
horSum[2] += third[i];
}
for (int i = 0; i < COLS; i++) {
verSum[i] = first[i] + second[i] + third[i];
}
diagSum1 = first[0] + second[1] + third[2];
diagSum2 = first[2] + second[1] + third[0];
bool correct = true;
for (int i = 0; i < ROWS; i++)
if (horSum[i] != sum) correct = false;
for (int i = 0; i < COLS; i++)
if (verSum[i] != sum) correct = false;
if (diagSum1 != sum || diagSum2 != sum)
correct = false;
if (correct)
cout << "It is a magic square";
else
cout << "Not a magic square";
}
Comments
Leave a comment