majic square

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  #include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

using namespace 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;
}
Topic archived. No new replies allowed.