Help with Magic Squares

Currently I'm making a magic squares program for C++ class but I cannot for the life of me get how to have it check that each number is unique or check columns or diagonals. Any help is appreciated.

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
#include<iostream>
using namespace std;
int main(){
    int length;
    int count = 0;
    int rowSum;
    bool rowCheck = false;

    cout << "What would you like the length of your magic square to be : ";
    cin >> length;

    int magicSquare[length][length];
    int rowTotals[length];

    for(int i = 0; i < length; i++){
        for(int j = 0; j < length; j++){
            cout << "Enter the value for row " << i << ", column " << j << " : ";
            cin >> magicSquare[i][j]; //fill the magic square
        }
    }

    for(int i = 0; i < length; i++){
        for(int j = 0; j < length; j++){
            cout << " | " << magicSquare[i][j] << " | "; //display the numbers in the magic square
        }
        cout << endl;
    }

    for(int h = 0; h < (length*length); h++){ //check if the numbers used were within the bounds of the size of the square
        for(int i = 0; i < length; i++){
            for(int j = 0; j < length; j++){
                if(h == magicSquare[i][j]){
                    count++;
                }
            }
        }
    }

    for(int i = 0; i < length; i++){ //add up the rows and store the totals in an array
        rowSum = 0;
        for(int j = 0; j < length; j++){
            rowSum = rowSum + magicSquare[i][j];
            rowTotals[i] = rowSum;
        }
    }

    for(int i = 0; i < length; i++){ //check to see if the rows are all the same
        for(int j = 0; j < length; j++){
            if(i != j && rowTotals[i] == rowTotals[j]){
                rowCheck = true;
            }
        }
    }

    if(count < (length*length)){
        cout << "The square is not magic. You used numbers greater than " << length*length << "."; //output if the user used numbers outside of the bounds of the square
    }
    else if(rowCheck == false) {
        cout << "Your rows don't add up.";
    }
    else{
        cout << "This square is possibly magic.";
    }
}
Topic archived. No new replies allowed.