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.
#include<iostream>
usingnamespace 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
}
elseif(rowCheck == false) {
cout << "Your rows don't add up.";
}
else{
cout << "This square is possibly magic.";
}
}