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;
}