Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads in 16 values from the user and tests whether they for

ID: 3625655 • Letter: W

Question

Write a program that reads in 16 values from the user and tests whether they form a magic square when put into a 4 x 4 array. You need to test two features:

• does each of the numbers 1 to 16 occur in the user input?
• when the numbers are put into a square, are the sums of the rows, columns and diagonals equal to each other?

Input: 16 numbers
Output: a printout of the 4 x 4 array, a message if the numbers 1 to 16 are not all in the array one time, a message of whether it is a magic square or not.

I think I have it set up wrong it is not reading in the 16 values correctly. Here is what I have.


int main()
{

// This is the 4x4 array.
int square[4][4] = {{16,3,2,13},{5,10,11,8},{9,6,7,12},{4,15,14,1}};
int magicNumber;
int sum;

// Assume it is a magic square until we find a row/col/diagonal that isn't
bool isMagicSquare = true;

// This is the number we will compare every other row/column/diagonal to
magicNumber = square[0][0] + square[0][1] + square[0][2] + square[0][3];

// First check all rows
for (int i = 0; i < 4; i++) {
sum = square[i][0] + square[i][1] + square[i][2] + square[i][3];
if (sum != magicNumber)
isMagicSquare = false;
}

// Now check all columns
for (int i = 0; i < 4; i++) {
sum = square[0][i] + square[1][i] + square[2][i] + square[3][i];
if (sum != magicNumber)
isMagicSquare = false;
}

// Now check the two diagonals
sum = square[0][0] + square[1][1] + square[2][2] + square[3][3];
if (sum != magicNumber)
isMagicSquare = false;

sum = square[0][3] + square[1][2] + square[2][1] + square[3][0];
if (sum != magicNumber)
isMagicSquare = false;

cout << "Array: ";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << square[i][j] << " ";
}
cout << endl;
}

if (isMagicSquare == true) {
cout << "This array is a magic square. ";
} else {
cout << "This array is not a magic square. ";
}
}

Explanation / Answer

#include<iostream>

using namespace std;

int main()
{

// This is the 4x4 array.
int square[4][4];// = {{16,3,2,13},{5,10,11,8},{9,6,7,12},{4,15,14,1}};
int magicNumber;
int sum;
static int p=1;
// Assume it is a magic square until we find a row/col/diagonal that isn't
bool isMagicSquare = true;
bool duplicate[17];
for(int i=1; i<16; i++)
duplicate[i] = false;


for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
       
        cout << "Enter Element " << (p++) << ":";
        cin >> square[i][j];
        if(square[i][j]>16 || square[i][j]<1 )
        {
        cout << "Found A number Out of Range (1-16) Exiting ... " <<endl;;
      // system("pause");
        return 0;                
        }
        if(duplicate[square[i][j]])
        {
        cout << "Duplicate Number found " <<endl;;
        cout << "can't Process towards Magic Square" <<endl;
      // system("pause");
        return 0;
        }
        duplicate[square[i][j]] = true;
        }
        }
       
       

// This is the number we will compare every other row/column/diagonal to
magicNumber = 34; // this magic number is constant. 16*17/2 * 4 = 34
// how u can assume that sum of first row will be magic
// if it is not a magic square ?
//square[0][0] + square[0][1] + square[0][2] + square[0][3];

// First check all rows
for (int i = 0; i < 4; i++)
{
sum = square[i][0] + square[i][1] + square[i][2] + square[i][3];
if (sum != magicNumber)
{
isMagicSquare = false;
break;
}
}

// Now check all columns
for (int i = 0; i < 4; i++)
{
    if(isMagicSquare)
    {
     sum = square[0][i] + square[1][i] + square[2][i] + square[3][i];
     if (sum != magicNumber)
     {
     isMagicSquare = false;
     break;
     }   
     }
}

// Now check the two diagonals
if(isMagicSquare)
{
sum = square[0][0] + square[1][1] + square[2][2] + square[3][3];
if (sum != magicNumber)
isMagicSquare = false;
}
if(isMagicSquare)
{
sum = square[0][3] + square[1][2] + square[2][1] + square[3][0];
if (sum != magicNumber)
isMagicSquare = false;
}

cout << "Array: ";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << square[i][j] << " ";
}
cout << endl;
}

if (isMagicSquare == true)
{
cout << "This array is a magic square. ";
}
else
{
cout << "This array is not a magic square. ";
}


// system("pause");
return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote