A magic square is an N by N array of integers. The sum of the values of each row, each
column, and the main diagonals are equal. Write a program that reads the size of a
square, then each row, and verifies if the square is a magic square using a function.
4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

Hi so thats the problem I have to solve, while using the above input my code still says its not a magic sqquare when it is. Also I need my code to say the size of the square, ie. 4x4 magic square etc.

anyone know how i can fix this?

#include<iostream>
using namespace std;
#include<fstream>
//load the array
void load2D(int &n, int square[10][10])
{
	ifstream infile;
	infile.open("data.txt");
	infile >>n;
	for (int i =0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			infile >>square[i][j];
		}
	}
}

//Print the 2 dimensional arrray
void print2D(int n, int square[10][10])
{

	for (int i =0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout <<square[i][j]<<" ";
		}
		cout <<endl;
	}
}
//Determines if it is a magic sqaure
bool isMagic(int n, int square[10][10])
{
     if (n == square[10][10])
              return true;
     else
              return false;
}

int main()
{
	int n;
	int square[10][10];
	load2D(n, square);
	print2D(n,square);
        bool isMagicSquare = isMagic(n, square);
if (isMagicSquare)
   cout << "Square is magic\n";
else
   cout << "Square is not magic\n";

system("pause");
return 0;
}

Anyone? :(

Where are you looping through the matrix and checking if it is a magic square?

// calculate sum of 1st row 
for (i = 0 to cols) {
        sum += mat[0][i];
}

// now check if all other rows and cols sum upto the same
for (i = 1 to rows) {
        sum1 = 0; 
        for (j = 0 to cols) {
                sum1 += mat[i][j];
        }
        if (sum1 != sum)
             return false;
} 

// the above code will do for rows. now, similarly, for columns.
....

// now, diagonals
for (i = 0 to rows)
       sum1 += mat[i][i];
if (sum1 != sum)
   return false;

// return true in the end. It means all conditions are satisfied.
return true;

can i inter-grade ur code into mine some how?

okay so i re-did my code entirely and have got it working, but now i need it to display out how big the magic square is ie. "this is a 4 by 4 magic square", anyone know what i need to add? thanks .

#include<iostream>
#include<fstream>
using namespace std;
const int dimension = 4;   // dimension for the array
typedef int Sq[dimension] [dimension];  // declare vector for type names
           
void ReadData(Sq square ) // read data from file
  {  ifstream inFile;
      char fileName[13] = "data.txt";
      inFile.open (fileName);  // open file for reading                
      for (int r = 0; r < dimension; r++)   // row loop
          for ( int c = 0; c < dimension; c++)  // column loop
                inFile >> square[r][c]; // read data into matrix
          inFile.close( ); // close the file                
   }

void Display ( const Sq square ) // display matrix
  { cout << " Magic Square Program " << endl << endl;
     for (int r = 0; r < dimension; r++)
       { for ( int c = 0; c < dimension; c++)
            { cout.width(6);  //set output width to 6 characters
               cout << square[r][c] << "\t ";  // display numbers
                }
           cout << endl;
       }    
   }

bool magicSquare( Sq square)  // add rows, columns, and diagonals
   { int firstSum = 0, sum;
      bool magic = true;

      for (int r = 0; r < dimension; r++) // add 1st column for a comparison
          firstSum += square[r][1]; 
       for (int r = 1; r < dimension; r++) // row loop first when adding rows
          { sum = 0;
             for ( int c = 0; c < dimension; c++)
                 sum += square[r][c];  // add row
             if ( sum != firstSum)  // check for magic failure
                  return (false);  // not magic
           }
                 
         for ( int c = 0; c < dimension; c++)   // column loop first when adding columns
           { sum = 0;
              for (int r = 0; r < dimension; r++)
                  sum += square[r][c];   // add columns
                  if ( sum != firstSum)  // check for magic failure
                   return (false);  // not magic
            }
         sum = 0;
         for (int r = 0; r < dimension; r++)  
              sum += square[r][r];   // add front diagonal
            if ( sum != firstSum)  // check for magic failure
               return (false);  // not magic
          sum = 0;
          for (int r = 0; r < dimension; r++)  
              sum += square[r][dimension - r - 1];   // add back diagonal
             if ( sum != firstSum)  // check for magic failure
              return (false);  // not magic
          else
              return (true);
          }  // end magicSquare function
                      
int main( )
  {
      Sq square;
     ReadData( square);  // read data from file
     Display ( square);  // display matrix
     if ( magicSquare(square) )   // check for magic property
        cout << "\n This Square is Magic \n " << endl;
     else
        cout << "\n This Square is Not Magic \n " << endl;
     system("Pause");
     return(0);
   }
const int dimension = 4;

So, is it always a 4x4 matrix?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.