#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
// Function prototypes
bool isMagicSquare(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);
bool checkRange(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size, int min, int
max);
bool checkUnique(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);
bool checkRowSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);
bool checkColSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);
bool checkDiagSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);
void fillArray(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);
void showArray(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);
int main()
{
/* Define a Lo Shu Magic Square using 3 parallel arrays corresponding
to each row of the grid */
int magicArrayRow1[COLS], magicArrayRow2[COLS], magicArrayRow3[COLS];
// Your code goes here
char input = 0;
//Function calls
do
{
fillArray(magicArrayRow1, magicArrayRow2, magicArrayRow3, COLS);
showArray(magicArrayRow1, magicArrayRow2, magicArrayRow3, COLS);
bool validMagicSquare = isMagicSquare(magicArrayRow1, magicArrayRow2, magicArrayRow3, COLS);
if (validMagicSquare == true)
cout << "This is a Lo Shu magic square." << endl;
else
{
cout << "This is not a Lo Shu magic square." << endl;
cout << "Try again? y/n" << endl;
cin >> input;
}
} while ((input == 'y') || (input == 'Y'));
return 0;
}
// Function definitions go here
//Fill array
void fillArray(int firstArray[], int secondArray[], int thirdArray[], int size)
{
int i;
int arrayInput;
//Array 1
for (i = 0; i < size; i++)
{
cout << "Enter the value for the first array at " << i << endl;
cin >> arrayInput;
firstArray[i] = arrayInput;
}
//Array 2
for (i = 0; i < size; i++)
{
cout << "Enter the value for the second array at " << i << endl;
cin >> arrayInput;
secondArray[i] = arrayInput;
}
//Array 3
for (i = 0; i < size; i++)
{
cout << "Enter the value for the third array at " << i << endl;
cin >> arrayInput;
thirdArray[i] = arrayInput;
}
}
//Show array
void showArray(int firstArray[], int secondArray[], int thirdArray[], int size)
{
int i;
//Array 1
for (i = 0; i < size; i++)
{
cout << firstArray[i] << " ";
}
//Array 2
cout << endl;
for (i = 0; i < size; i++)
{
cout << secondArray[i] << " ";
}
cout << endl;
//Array 3
for (i = 0; i < size; i++)
{
cout << thirdArray[i] << " ";
}
}
//Check range
bool checkRange(int firstArray[], int secondArray[], int thirdArray[], int size, int min, int max)
{
int i;
bool status = true;
while (status == true)
{
//Check first array
for (i = 0; i < size; i++)
{
if ((firstArray[i] >= min) && (secondArray[i] <= max))
status = true;
else
status = false;
}
//Check second array
for (i = 0; i < size; i++)
{
if ((secondArray[i] >= min) && (secondArray[i] <= max))
status = true;
else
status = false;
}
//Check third array
for (i = 0; i < size; i++)
{
if ((thirdArray[i] >= min) && (secondArray[i] <= max))
status = true;
else
status = false;
}
}
return status;
}
//Check if unique
bool checkUnique(int firstArray[], int secondArray[], int thirdArray[], int size)
{
bool status;
//First array
if ((firstArray[0] != firstArray[1]) && (firstArray[2] != firstArray[0]) && (firstArray[1] != firstArray[2]) )
status = true;
else
status = false;
//Second array
if ((secondArray[0] != secondArray[1]) && (secondArray[2] != secondArray[0]) && (secondArray[1] != secondArray[2]) )
status = true;
else
status = false;
//Third array
if ((thirdArray[0] != thirdArray[1]) && (thirdArray[2] != thirdArray[0]) && (thirdArray[1] != thirdArray[2]) )
status = true;
else
status = false;
return status;
}
//Check the row sum
bool checkRowSum(int firstArray[], int secondArray[], int thirdArray[], int size)
{
int i;
int rowSum1 = 0;
for (i = 0; i < size; i++)
{
rowSum1 += firstArray[i];
}
int rowSum2 = 0;
for (i = 0; i < size; i++)
{
rowSum2 += secondArray[i];
}
int rowSum3 = 0;
for (i = 0; i < size; i++)
{
rowSum3 += thirdArray[i];
}
if ((rowSum1 == rowSum2) && (rowSum2 == rowSum3))
return true;
else
return false;
}
//Check the column sum
bool checkColSum(int firstArray[], int secondArray[], int thirdArray[], int size)
{
int colSum1 = 0;
colSum1 = (firstArray[0] + secondArray[0] + thirdArray[0]);
int colSum2 = 0;
colSum2 = (firstArray[1] + secondArray[1] + thirdArray[1]);
int colSum3 = 0;
colSum3 = (firstArray[2] + secondArray[2] + thirdArray[2]);
if ((colSum1 == colSum2) && (colSum2 == colSum3))
return true;
else
return false;
}
//Check the diagonal sum
bool checkDiagSum(int firstArray[], int secondArray[], int thirdArray[], int size)
{
int diagSum1 = 0;
diagSum1 = (firstArray[0] + secondArray[1] + thirdArray[2]);
int diagSum2 = 0;
diagSum2 = (firstArray[2] + secondArray[1] + thirdArray[0]);
if (diagSum1 == diagSum2)
return true;
else
return false;
}
//Check if magic square
bool isMagicSquare(int firstArray[], int secondArray[], int thirdArray[], int size)
{
bool validRange = checkRange(firstArray, secondArray, thirdArray, COLS, MIN, MAX);
bool isUnique = checkUnique(firstArray, secondArray, thirdArray, COLS);
bool validRowSum = checkRowSum(firstArray, secondArray, thirdArray, COLS);
bool validColSum = checkColSum(firstArray, secondArray, thirdArray, COLS);
bool validDiagSum = checkDiagSum(firstArray, secondArray, thirdArray, COLS);
if ((validRange == true) && (isUnique == true) && (validRowSum == true) && (validColSum == true) && (validDiagSum == true))
return true;
else
return false;
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}