Help with magic square test program

Hello, i was trying to create a program that adds the diagonals and columns of a square, and if those are equal output the sum else output "-1"

For the question i was doing, the minimum dimension were 3*3 to 10*10 maximum so i hardcoded the conditions for each case i.e 8 if statements could someone please show me an easier way of solving this problem, i know there is another way:)

~Thanks.

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Test if numbers read from file form a magic Sqaure!

1. A square is magic if the sum of the diagonal is equal to the some of both the first rows and the
 second last column

2. Assume that for this program the input.txt may only have a square with maximum dimensions
of 10*10 and minimum dimensions of 3*3
*/

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

void isMagic()
{
    vector <int> data;
    int cntr = 0, num = 0, dsum = 0, hsum = 0, csum = 0, k =0;
    //hsum == horizontal or "row" sum. csum = column sum, dsum = sum of the diagonals!
    ifstream infile("input.txt", ios::in);

    while(infile>>num) {
        data.push_back(num);
        cntr++;
    }
    infile.close();
    /*
                Create condition for 3*3 - 10*10 conditions
    */
    ofstream output("output.txt", ios::out);

//case 3*3
    if (cntr <= 3*3) {
        num = 3;

        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++) {
            k = 4*j-2;
            hsum += data[j];
            if(j>0) {
                csum +=data[k];
            }
        }
        if(dsum==hsum && dsum==csum)
            output<<hsum;
        else
            output<<"-1";
    }
//case 4*4
    else if (cntr <= 4*4) {
        num = 4;
        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++) {
            k = 4*j-2;
            hsum += data[j];
            if(j>0) {
                csum +=data[k];
            }
        }
        if(dsum==hsum && dsum==csum)
            output<<hsum;
        else
            output<<"-1";
    }

//case 5*5
    else if (cntr <= 5*5) {
        num = 5;
        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++) {
            k = 4*j-2;
            hsum += data[j];
            if(j>0) {
                csum +=data[k];
            }
        }
        if(dsum==hsum && dsum==csum)
            output<<hsum;
        else
            output<<"-1";
    }

//case 6*6
    else if (cntr <= 6*6) {
        num = 6;
        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++) {
            k = 4*j-2;
            hsum += data[j];
            if(j>0) {
                csum +=data[k];
            }
        }
        if(dsum==hsum && dsum==csum)
            output<<hsum;
        else
            output<<"-1";
    }

//case 7*7
    else if (cntr <= 7*7) {
        num = 7;
        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++) {
            k = 4*j-2;
            hsum += data[j];
            if(j>0) {
                csum +=data[k];
            }
        }
        if(dsum==hsum && dsum==csum)
            output<<hsum;
        else
            output<<"-1";
    }

//case 8*8
    else if (cntr <= 8*8) {
        num = 8;
        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++) {
            k = 4*j-2;
            hsum += data[j];
            if(j>0) {
                csum +=data[k];
            }
        }
        if(dsum==hsum && dsum==csum)
            output<<hsum;
        else
            output<<"-1";
    }

//case 9*9
    else if (cntr <= 9*9) {
        num = 9;
        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++) {
            k = 4*j-2;
            hsum += data[j];
            if(j>0) {
                csum +=data[k];
            }
        }
        if(dsum==hsum && dsum==csum)
            output<<hsum;
        else
            output<<"-1";
    }

//case 10*10
    else if (cntr <= 10*10) {
        num = 10;
        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++) {
            k = 4*j-2;
            hsum += data[j];
            if(j>0) {
                csum +=data[k];
            }
        }
        if(dsum==hsum && dsum==csum)
            output<<hsum;
        else
            output<<"-1";
    }//End of conditions
    output.close();
}

int main()
{
    isMagic();
}
Last edited on
Yes there is, use sqrt() and the 'and' operator:

if((cntr >= 3*3) && (cntr <= 10*10))

And set num = sqrt(cntr).

Also, add return 0; to the end of your main(), since main() is declared to return a type int.

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
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath> //Needed for sqrt()

using namespace std;

void isMagic()
{
    vector <int> data;
    int cntr = 0, num = 0, dsum = 0, hsum = 0;
    ifstream infile("input.txt", ios::in);

    while(infile>>num) {
        data.push_back(num);
        cntr++;
    }
    infile.close();
    /*
                Create condition for 3*3 - 10*10 conditions
    */
    ofstream output("output.txt", ios::out);

    if((cntr >= 3*3) && (cntr <= 10*10))
    {
        num = static_cast<int>(sqrt(cntr));

        for(int i = 0; i<cntr; i+=(num+1))
            dsum += data[i];
        for (int j = 0; j<num; j++)
            hsum += data[j];
        if(dsum==hsum)
            output<<hsum;
        else
            output<<"-1";
    }
    
    output.close();
}

int main()
{
    isMagic();
    return 0;
}
Last edited on
:), WhaAaaAaaAAaat!!!!, thanks a lot mate, cant believe i wasted my life typing my code when there was something this simple!
You are welcome :). I was a beginner at some point too ;).
yeah gues so, could u help me with an algorithm for calculating the sum of the second last column of each square, m really struggling with it.
Last edited on
Can I see your input file?
its cool now, i figured out the cntr thing,
theses are my inputs btw:

1
2
3
4
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Well here is my algorithm for calculating the sum of the 2nd last column.

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
//Stormboy's algorithm for summing the 2nd last column

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;

int getSum(string filename)
{
    vector<int> numbers;
    int num_from_file;
    int sum = 0;

    ifstream myepicfile(filename.c_str(), ios::in);

    while(!myepicfile.eof())
    {
        myepicfile>>num_from_file;
        numbers.push_back(num_from_file);
    }

    int square_side = (int)sqrt((float)numbers.size());

    int j = 1;

    for(int i = square_side-1; j<=square_side; i+=square_side)
    {
        sum+=numbers[i-1];
        j++;
    }
    return sum;
}

int main()
{
    int thesum = getSum("inp.txt");
    cout << "Sum of the 2nd last column: " << thesum << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.