Question
// can anyone add some codes that requests the user if they want to try again and let them insert the new trial. // the
// can anyone add some codes that requests the user if they want to try again and let them insert the new trial.
// the codes terminates if wrong attempt inserted by showing a message it is wrong.
include
int magicArray[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { cout << "Enter values ROW " << (i + 1) << " COL " << (j + 1) << ": " << endl; } cout << endl; } for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { cin >> magicArray[i][j]; } }
showResult(magicArray); cout << "Programmar name: xxxxx" << endl; cout << "Due Date: 05/08/2022 " << endl; cout << "Project 5 " << endl; return 0; } void showResult(int values[][COLS]) { // Determine if the array is a Lo Shu Magic Square. if (isMagicSquare(values)) { cout << "This is a Lo Shu magic square. "; } else { cout << "This is not a Lo Shu magic square. "; } } void showArray(int values[][COLS]) {
for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { cout << values[row][col] << " "; }
cout << endl; } } bool isMagicSquare(int values[][COLS]) {
bool status = false;
bool isInRange = checkRange(values);
bool isUnique = checkUnique(values); bool isEqualRows = checkRowSum(values); bool isEqualCols = checkColSum(values); bool isEqualDiag = checkDiagSum(values);
if (isInRange && isUnique && isEqualRows && isEqualCols && isEqualDiag) { status = true; }
return status; } bool checkRange(int values[][COLS]) {
bool status = true; for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) {
if (values[row][col] < MIN || values[row][col] > MAX) {
status = false; } } }
return status; } bool checkUnique(int values[][COLS]) { int count = 0; int num = 1; bool status = true; for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) { if (values[i][j] == num) count++; } if (count > 1) status = false; num++; }
return status; } bool checkRowSum(int values[][COLS]) {
bool status = true; // Calculate the sum of the values in the rows. int sumRowA = values[0][0] + values[0][1] + values[0][2]; int sumRowB = values[1][0] + values[1][1] + values[1][2]; int sumRowC = values[2][0] + values[2][1] + values[2][2]; if ((sumRowA != sumRowB) || (sumRowA != sumRowC) || (sumRowB != sumRowC)) {
status = false; }
return status; } bool checkColSum(int values[][COLS]) { bool status = true; // Calculate the sum of the values int sumColA = values[0][0] + values[1][0] + values[2][0]; int sumColB = values[0][1] + values[1][1] + values[2][1]; int sumColC = values[0][2] + values[1][2] + values[2][2]; if ((sumColA != sumColB) || (sumColA != sumColC) || (sumColB != sumColC)) { status = false; } return status; } bool checkDiagSum(int values[][COLS]) {
bool status = true; int sumDiagA = values[0][0] + values[1][1] + values[2][2]; int sumDiagB = values[2][0] + values[1][1] + values[0][2]; if (sumDiagA != sumDiagB) {
status = false;
} return status; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started