The program i wrote is to check whether the matrix is a magic square. However my program is not complete, because while the program check for duplicates, the code does not check for missing numbers in the sequence, can anyone complete the code for me? I know i need to use bool function but i don't know how to do it.
The code itself can do the following
-> check make sure each column, row and diagonals are equals
-> make sure no number is repeated.
}
int a = matrix[0][0];//11
int b = matrix[0][1];//12
int c = matrix[0][2];//13
int d = matrix[1][0];//21
int e = matrix[1][1];//22
in! t f = matrix[1][2];//23
int g = matrix[2][0];//31
int h = matrix[2][1];//32
int i = matrix[2][2];//33
if ((a + b + c == d + e + f)&& (d + e + f == g + h + i)&& (a + d + g == b + e + h)&& (b + e + h== c + f + i) && (a + e + i == g + e + c)&& (a!=b!=c!=d!=e!=f!=g!=h!=i)! )
cout <<" this is a magic square"<<endl;
else
cout <<" this is not a magic square"<<endl;
For the "make sure no numbers are repeated" part, you can use a boolean array with size nine. For each value in the square, set array[value-1] to one. You know the square is valid if it contains no zeros.
Declare a boolean array of size 'x', or 9 in your case: bool array[x] = {0} //{0} will fill the array with zeros
In a boolean array each element is either a one or a zero, representing yes or no, true or false etc.