1
votes

I have a program that attempts to find a magic square. A square matrix of numbers where all the rows,columns,diagonals add up to the same number.

So far I have a 3x3 array that successfully populates with truly random numbers. It appears to work properly in doing so, but when I include the program in a while(true) loop, the program runs on forever without finding the magic square, and I think it's because my conditional statement is written incorrectly. Here's it is:

if (row0 == row1 == row2 == col0 == col1 == col2 == dia1 == dia2) {
    cout << "We have a magic square!" <<  endl;
    cout << troysArray[i][j];
    cout << "";
    break;
} 

The whole program is here:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//Defining the sum function, which takes 3 integers as arguments.
int addnums(int x,int y,int z){
    int result = x + y + z;
    return result;}

int main() {
    srand(time(0));

//Initial array contents before random numbers are substituted
int troysArray[3][3] = {
{1,3,2},
{4,6,5},
{7,9,8},
};

int i;
int j;

int row0;
int zero_zero;
int zero_one;
int zero_two;

int row1;
int one_zero;
int one_one;
int one_two;

int row2;
int two_zero;
int two_one;
int two_two;

int col0;
int col1;
int col2;

int dia1;
int dia2;
int sum = row0;

while(true) {
for (i = 0;i < 3;i++){
    for (j = 0;j < 3;j++){
        //Generating random numbers between 1-9, with which to populate troysArray.
        troysArray[i][j] = 1 + (rand() % 9);
        cout << troysArray[i][j];
        cout << "";
    //If all the rows,columns, and diagonals are equal,we have a magic square!
    if (row1 == sum && row2 == sum && col0 == sum && col1 == sum && col2 == sum &&
        dia1 == sum && dia2 == sum) {
        cout << "We have a magic square!" << endl;
        cout << troysArray[i][j];
        cout << "";
        break;}
    }
    cout << endl;

}
}
//Adding up row 0 (top row):
zero_zero = troysArray[0][0];
zero_one = troysArray[0][1];
zero_two = troysArray[0][2];
row0 = addnums(zero_zero,zero_one,zero_two);
cout << "The sum of row 0 equals: " << zero_zero + zero_one + zero_two << endl;

//Adding up row 1 (middle row):
one_zero = troysArray[1][0];
one_one = troysArray[1][1];
one_two = troysArray[1][2];
row1 = addnums(one_zero,one_one,one_two);
cout << "The sum of row 1 equals: " << one_zero + one_one + one_two << endl;

//Adding up row 2 (bottom row):
two_zero = troysArray[2][0];
two_one = troysArray[2][1];
two_two = troysArray[2][2];
row2 = addnums(two_zero,two_one,two_two);
cout << "The sum of row 2 equals: " << two_zero + two_one + two_two << endl;

cout << "\n";

//Adding up col 0 (Left):
col0 = addnums(zero_zero,one_zero,two_zero);
cout << "The sum of col 0 equals: " << zero_zero + one_zero + two_zero << endl;

//Adding up col 1 (Middle):
col1 = addnums(zero_one,one_one,two_one);
cout << "The sum of col 1 equals: " << zero_one + one_one + two_one << endl;

//Adding up col 2 (Right):
col2 = addnums(zero_two,one_two,two_two);
cout << "The sum of col 2 equals: " << zero_two + one_two + two_two << endl;

cout << "\n";

//Adding up tL-bR diagonal (dia 1):
dia1 = addnums(zero_zero,one_one,two_two);
cout << "The sum of dia 1 equals: " << zero_zero + one_one + two_two << endl;

//Adding up bL-tR diagonal (dia 2):
dia2 = addnums(two_zero,one_one,zero_two);
cout << "The sum of dia 2 equals: " << zero_two + one_one + two_zero << endl;

return 0;
}
1

1 Answers

5
votes

The Equality operator doesn't work in C and C++ the way you think it does.

It is evaluated from left to right, therefore a == b == c becomes (a == b) == c, or (true/false) == c.

The correct way to solve this is to compare each value individually:

int sum = row0;
if (row1 == sum && row2 == sum && ...) { ... }

Update:

You can create your own multiple value comparator like this:

template <class Arg, class... Args>
bool all_equal(Arg&& arg, Args&&... args) {
   static_assert(sizeof...(args), "At least 2 arguments expected");
   return (arg == args && ...);
}

// Example
if (all_equal(row0, row1, ...)) {}