-1

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.

Nirl
  • 3
  • 3

1 Answers1

0

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:

Md. Faisal Habib
  • 1,014
  • 1
  • 6
  • 14
  • Where should I put this? Once I tried replacing the main, it just printed out the whole thing? – Nirl Mar 14 '22 at 05:32
  • @Nirl please check the code again. Although I've mentioned the details previously, still I've edited the code. – Md. Faisal Habib Mar 14 '22 at 08:42
  • I have posted my update, it is showing the whole array instead of the correct number in the index. – Nirl Mar 14 '22 at 16:06
  • @Nirl what does the correct number mean? The magic square is not a single number. Can you please clarify more about your requirements? - What is your main objective? - For the given matrix, why the output is 83? – Md. Faisal Habib Mar 14 '22 at 18:22
  • I may have misunderstood the whole thing, the question was to find the biggest number and an adjacent second biggest number and merge them, with 2 conditions. 1. consecutive numbers are arranged from top to bottom and 2. consecutive numbers are arranged from left to right (so no right to top and no bottom to top). So in this case, the biggest number after merging is 83. Should I start a new thread? – Nirl Mar 14 '22 at 18:45
  • @Nirl I guess it would be better to give a new post for this question. This isn't related to Magic Square. Please give a post with examples. – Md. Faisal Habib Mar 14 '22 at 19:29