hey guys am i seriously need help....am doing a majic square problem........i cant
seem to get the correct ansa am getting the mequation even if its nt a majic square plz help
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
usingnamespace std;
///////////////////////////recursive function to calculate power
int calculate(int mequation,vector<int>data)
{
int asize=sqrt(data.size());
int array[10][10];
int sumrow=0; //sums all the rows
int sumcol=0; //sums all the columns
int sumfdiag=0; //sums the first diagonal
int sumldiag=0; //sums the last diagonal
mequation=(int)(asize*(pow(asize,2)+1))/2; //equatin for majic square only that matches every row,column and diagonal
////////////////checking the rows first
for(int i=0; i<asize; i++)
{
sumrow=0;
for(int j=0; j<asize; j++)
{
sumrow=sumrow+array[i][j];
}
if(sumrow==mequation);
return 0;
}
///////////////checking the columns
for(int i=0; i<asize; i++)
{
sumcol=0;
for(int j=0; j<asize; j++)
{
sumcol=sumcol+array[j][i];
}
if(sumcol==mequation)
return 0;
}
////////////////////checking first diagonal
for(int i=0; i<asize; i++)
{
sumfdiag=sumfdiag+array[i][i];
}
if(sumfdiag==mequation)
return 0;
///////////////////checking second diagonal
int j=0;
for(int i=asize-1; i>=0; i--)
{
sumldiag=sumldiag+array[j][i];
j++;
}
if(sumldiag==mequation)
return 0;
return 1;
}
int main()
{
vector<int>data;
int mequation=0;
int d=0;
int array[10][10];
ifstream infile("input.txt");
int counter=0;
while(infile>>d)
{
data.push_back(d);
}
int asize=sqrt(data.size());
mequation=(int)(asize*(pow(asize,2)+1))/2;
/////////////////////////////////////////////////////////
for(int i=0; i<asize; i++)
{
for(int j=0; j<asize; j++)
{
array[i][j]=data.at(counter);
counter++;
}
}
int flag=0;
flag=calculate(mequation,data);
if(flag)
{
cout<<"-1"<<endl;
}
else
cout<<mequation<<endl;
}