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

Implement the isMagic function in C++ and do not use any other library functions

ID: 3864896 • Letter: I

Question

Implement the isMagic function in C++ and do not use any other library functions other than

#include <iostream>
using namespace std;

In recreational mathematics, a magic square is an arrangement of distinct numbers (i.e. each number is used once usually integers, in a square grid, where the numbers in each row and in each column, and the numbers in the main and secondary diagonals, all add up to the same number Write a function called isMagic that takes a 2D array and returns true if the 2D array is a magic square and false otherwise.

Explanation / Answer

#include<iostream>
using namespace std;

int inMagic(int n)
{
   cout<<"Enter the elements"<<endl;
   int a[n][n];
   int i,j;
   for(i=0;i<n;i++)
   for(j=0;j<n;j++)
   cin>>a[i][j];
   int sum=0;
   for(i=0;i<n;i++)
   {
   sum=sum+a[0][i];
     // cout<<sum<<endl;
   }
   int sum1=0;
   for(i=1;i<n;i++)
   {
   sum1=0;
   for(j=0;j<n;j++)
   sum1+=a[i][j];
   if(sum1!=sum)
   {
   cout<<"false"<<endl;
   return 0;
   }
   }
   for(i=0;i<n;i++)
   {
   sum1=0;
   for(j=0;j<n;j++)
   sum1+=a[j][i];
   if(sum1!=sum)
   {
   cout<<"false"<<endl;
   return 0;
   }
   }
   sum1=0;
   for(i=0;i<n;i++)
   {
   sum1+=a[i][i];}
     if(sum1!=sum)
   {
   cout<<"false"<<endl;
   return 0;
   }
   sum1=0;

   for(i=0;i<n;i++)
   {
   sum1+=a[i][n-1-i];
   }
     if(sum1!=sum)
   {
   cout<<"false"<<endl;
   return 0;
   }

   cout<<"true"<<endl;
   return 0;
}

int main()
{
   int n;
   cout<<"Enter the size of matrix"<<endl;
   cin>>n;


   inMagic(n);
   return 0;
}