user input for a sum check. any suggestions?

I need help writing this program

magic square checker

Magic squares are a square grid of consecutive numbers with the property that every row, column and diagonal sums to the same number.
It should look like this:

Welcome to the magic square checker. please enter your magic square

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

then replies if this is a magic square or not.

Last edited on
your gonna need to post what you've started with first. How much have you written? Which direction are you going in?
If you only need to read squares of predetermined size (i.e. always 4*4) then it's dead easy, just use a 2D array such as:

int square[4][4];

If the squares can vary in size, it gets a bit harder and I suggest you read the data from a text file, and instead of 2D arrays you should use one of the STL containers:

1
2
3
4
5
#include <vector> // this should be the best choice for you

// ...

std::vector<std::vector<int> > square;


It appears uglier this way, but at least you don't have to manage the size anymore: you'll just read data and the vectors will resize accordingly.
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
#include <iostream>
#include <iomanip>

using namespace std;

int main() 
{

	cout << "Welcome to the Magic Square Checker! Please enter your magic square: "
	
	//cin >>
	
	int a0, a1, a2, a3;
	int b0, b1, b2, b3;
	int c0, c1, c2, c3; 
	int d0, d1, d2, d3;
	// Here we are setting variables
	
	a0 = 1; a1 = 2; a2 = 3; a3 = 4;
	b0 = 1; b1 = 2; b2 = 3; b3 = 4;
	c0 = 1; c1 = 2; c2 = 3; c3 = 4;
	d0 = 1; d1 = 2; d2 = 3; d3 = 4;
	
	//  horizontal sum
	
	sum1 = a0 + a1 + a2 + a3;
	sum2 = b0 + b1 + b2 + b3;
	sum3 = c0 + c1 + c2 + c3;
	sum4 = d0 + d1 + d2 + d3;
	
	//  vertical sum
	
	vsum1 = a0 + b0 + c0 + d0;
	vsum2 = a1 + b1 + c1 + d1;
	vsum3 = a2 + b2 + c2 + d2;
	vsum4 = a3 + b3 + c3 + d3;
	
	//  diagonal sum
	
	dsum1 = a0 + b1 + c2 + d3;
	dsum2 = a3 + b2 + c2 + d3;
	
       // check 

	if(sum1 == sum2 == sum3 == sum4  // if statement
		== vsum1 == vsum2 == vsum3 
		== vsum4 == dsum1 == dsum2) {  
		cout << " Yes this is a magic square" ; //Than this is a magic square
		}  
		{ else  
		cout << " No this is not a magic square" ; // If not 
		
		}
	 
	// It think there needs to be a loop?
	
	return0;
	
}[code]
[/code]
Last edited on
Are the squares you read of a predetermined size, or can they be any size?

Also, please use code tags.
http://cplusplus.com/articles/jEywvCM9/
I worked on it some more.
This is a four by four row magic square, the user must input the numbers and this program will check if every row, column and diagonal sums to the same number. or not.
You are doing all the sums manually... which is why I suggested a 2D array (or matrix if you will) in the first place. With a matrix all you need to do is play around with indices in a couple of for() loops.

(The code below was not tested.)

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
int square[4][4];

// reading data from keyboard
for (int i=0; i<4; ++i)
    for (int j=0; j<4; ++j)
    {
        cout << "square[" << i << "][" << j << "] = ";
        cin >> square[i][j];
    }

// ...

// magic testing function
// will return true if square is magic, false otherwise
bool is_magic(const int square[4][4])
{
    // all other sums need to be equal to this
    int expected_sum = 0;

    // compute the expected sum
    for (int j=0; j<4; ++j)
        expected_sum += square[0][j];

    int temp_sum = 0;

    // check the lines
    // (start from the second line because expected_sum already sums up the first)
    for (int i=1; i<4; ++i)
    {
        for (int j=0; j<4; ++j)
            temp_sum += square[i][j];

        if (temp_sum != expected_sum)
            return false; // quit on the first bad sum

        temp_sum = 0; // clear the temporary sum for next line
    }

    // check the columns
    for (int j=0; j<4; ++j)
    {
        for (int i=0; i<4; ++j)
            temp_sum += square[i][j];

        if (temp_sum != expected_sum)
            return false;

        temp_sum = 0;
    }

    // check the diagonals

    // diagonal one, formula square[k][k]
    // square[0][0] + square[1][1] + square[2][2] + ... + square[n-1][n-1]
    for (int k=0; k<4; ++k)
        temp_sum += square[k][k];

    if (temp_sum != expected_sum)
        return false;

    temp_sum = 0;

    // diagonal two, formula square[k][n-1-k]
    // square[0][n-1] + square[1][n-2] + square[2][n-3] + ... + square[n-1][0]
    for (int k=0; k<4; ++k)
        temp_sum += square[k][4-1-k];

    if (temp_sum != expected_sum)
        return false;

    return true; // if we got here, it can only be that the square is magic...
    // either that, or I messed up some checks
}

// ...

if (is_magic(square))
    cout << "Yes this is a magic square.\n";
else
    cout << "No this is not a magic square.\n";


The problem with the above code (which wasn't tested) is that the size of the square is hardcoded as 4. The good thing is that, unlike in your solution, if you figure out how to remove the 4 and use a variable n instead, the program will work for squares of any size n.
Topic archived. No new replies allowed.