Search code examples
c++magic-square

How do I print out the value that make magic square?


I have tried this code that I found online and it worked, but I want it to print out which number makes magic square. In this case it is 83, so instead of cout<<"Magic Square", how do I change it to show 83 instead?

Thank you in advance.

 
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
using namespace std;
 
// Returns true if mat[][] is magic
// square, else returns false.
bool isMagicSquare(int mat[][3])
{
    int n = my_sizeof(mat)/my_sizeof(mat[0]);
    // calculate the sum of
    // the prime diagonal
    int i=0,j=0;
    // sumd1 and sumd2 are the sum of the two diagonals
    int sumd1 = 0, sumd2=0;
    for (i = 0; i < n; i++)
    {
        // (i, i) is the diagonal from top-left -> bottom-right
        // (i, n - i - 1) is the diagonal from top-right -> bottom-left
        sumd1 += mat[i][i];
        sumd2 += mat[i][n-1-i];
    }
    // if the two diagonal sums are unequal then it is not a magic square
    if(sumd1!=sumd2)
        return false;
 
    // For sums of Rows
    for (i = 0; i < n; i++) {
         
        int rowSum = 0, colSum = 0;   
        for (j = 0; j < n; j++)
        {
            rowSum += mat[i][j];
            colSum += mat[j][i];
        }
        if (rowSum != colSum || colSum != sumd1)
            return false;
    }
   return true;
}
 
// driver program to
// test above function
int main()
{
    int mat[3][3] = {{ 1, 5, 6 },
                    { 8, 2, 7 },
                    { 3, 4, 9 }};
     
    if (isMagicSquare(mat))
        cout << "Magic Square";
    else
        cout << "Not a magic Square";
     
    return 0;
}

As per suggested, I have tried to change it to:

int main()
{
    int mat[3][3] = {{ 1, 5, 6 },
                    { 8, 2, 7 },
                    { 3, 4, 9 }};
     
 if (isMagicSquare(mat))
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            cout<< mat[i][j] << ' ';
        }
        cout<< endl;
    }
}
    else
        cout << "Not a magic Square";
     
    return 0;
}

But it showed the whole array instead of the correct index in the array. I am sorry, I am somewhat new at the whole thing.

The result is showing up as: 1 5 6 8 2 7 3 4 9

Did I changed it in the wrong place? Or is there any further reading that I should read. Any helps would be appreciate.

The result that I am expecting is

83

as it is the number in the index that is the magic number.


Solution

  • If the given square is a magic square, that means when isMagicSquare(mat) is true, then iterate through the given square and print each of the values.

    To do that, you'll have to learn how to print a 2D array.

    In your case, you can do like below:

    if (isMagicSquare(mat))
    {
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                cout<< mat[i][j] << ' ';
            }
            cout<< endl;
        }
    }
    

    Please check the below resources to learn more about 2D array: