Hi, trying to set up a boolean return function that lets me know whether the contents of a multidimensional array are a "magic square" (sum of all rows, columns, and diagonals are equal). I set it up and tested it (can't get my VSCode debugger to stop at breakpoints for some reason) and the bool is outputting false when I've verified the array sums visually. Is there something wrong here I'm not seeing? I must have gone over this code like 10 times already.
#include <iostream>
usingnamespace std;
//C++ program to determine if a multidimensional array is a "magic square"
//(sum of all lines and diagonals is the same)
//set the size of the array so it can be modified if desired (9x9 square, 5x5 square, etc)
#define L 3
/** Determines whether a square two-dimensional array is a "perfect" square
* @param int square_array[][L] (array values to be verified)
* @return result (true/false boolean result)
*/
bool isMagicSquare(int square_array[][L])
{
//DIAGONALS
//check to see if sums of diagonals match
int sum = 0, sum2 = 0;
//first diagonal
for(int i = 0; i < L; i++)
{
sum += square_array[i][i];
}
//second diagonal
for(int i = 0; i < L; i++)
{
sum2 += square_array[i][L-1-i];
}
//equity check
if(sum != sum2)
{
returnfalse;
}
//check row sums (nested for loop)
int rowSum = 0;
for(int i = 0; i < L; i++)
{
for(int j = 0; j < L; j++)
{
rowSum += square_array[i][j];
}
//check each row sum to make sure it's equal to diagonal sums
if(rowSum != sum)
{
returnfalse;
}
}
//check column sums (another nested loop)
int colSum = 0;
for(int i = 0; i < L; i++)
{
for(int j = 0; j < L; j++)
{
colSum += square_array[j][i];
}
//check each column to make sure it's equal to diagonal sums
if(colSum != sum)
{
returnfalse;
}
}
//if boolean is never switched to false, return true result
returntrue;
}
int main()
{
int square[][L] = { { 2, 7, 6 },
{ 9, 5, 1 },
{ 4, 3, 8 } };
if(isMagicSquare(square))
{
cout << "Array is a magic square.";
}
else
{
cout << "Array is NOT a magic square.";
}
}
It's not about scope. It's that you need to reset the sum to 0 for every iteration of the outer loop, otherwise you're just adding to the sum of the last row/col.
Some little points:
L is better named Size and should be a const int instead of a define.
constint Size = 3;
i and j would be better named row and col (in the double-for loops).
1 2 3 4 5 6 7 8
for (int row = 0; row < Size; row++)
{
int rowSum = 0;
for (int col = 0; col < Size; ++col)
rowSum += square[row][col];
if (rowSum != sum)
returnfalse;
}
Got it. I'm also having issues with setting it up so the user can input the numbers. For some reason, the array is just filled with the last int entered:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int square[L][L] = {{2, 7, 6},
{9, 5, 1},
{4, 3, 8}};
int count = 0;
while(count < L * L)
{
int input;
cout << "Enter a number 1-" << L * L << ": ";
cin >> input;
for(int i = 0; i < L; i++)
{
for(int j = 0; j < L; j++)
{
square[i][j] = input;
}
}
count++;
}
This gives me an output of
8 8 8
8 8 8
8 8 8
uuuuugghhhh!
EDIT: I read the tutorial on this site about filling multidimensional arrays, and my code appears to be solid. Now I'm reaaaal confused
int main()
{
int square[L][L];
int count = 0;
while(count < L * L)
{
int input;
cout << "Enter a number 1-" << L * L << ": ";
cin >> input;
//check to make sure the input is within the specified range
if(inRange(input))
{
for(int i = 0; i < L; i++)
{
for(int j = 0; j < L; j++)
{
square[i][j] = input;
}
}
count++;
}
else
{
cout << "Please make a valid entry (1-" << L * L << ")" << endl;
}
}
print_array(square);
return 0;
}
The while loop doesn't make sense.
Just use the double loop that goes through all the elements in square. Put the cin in the inner loop since you need to read a new value for each inner iteration.
#include <iostream>
usingnamespace std;
//C++ program to determine if a multidimensional array is a "magic square"
//(sum of all lines and diagonals is the same)
//set the size of the array so it can be modified if desired (9x9 square, 5x5 square, etc)
#define L 3
/** Prints a two-dimensional array
* @param int args[][] (array to print)
*/
void print_array(int args[L][L])
{
for(int i = 0; i < L; i++)
{
for(int j = 0; j < L; j++)
{
cout << args[i][j] << " ";
if(j == L - 1)
{
cout << endl;
}
}
}
}
/**Determines whether an integer is within the range of 1-total amt
* @param int a (integer to be analyzed)
* @return true/false based on integer
*/
bool inRange(int a)
{
if(a < 1 || a > L * L)
{
returnfalse;
}
returntrue;
}
/** Determines whether a square two-dimensional array is a "perfect" square
* @param int square_array[][L] (array values to be verified)
* @return result (true/false boolean result)
*/
bool isMagicSquare(int square_array[][L])
{
//DIAGONALS
//check to see if sums of diagonals match
int sum = 0, sum2 = 0;
//first diagonal
for(int i = 0; i < L; i++)
{
sum += square_array[i][i];
}
//second diagonal
for(int i = 0; i < L; i++)
{
sum2 += square_array[i][L-1-i];
}
//equity check
if(sum != sum2)
{
returnfalse;
}
//check row sums (nested for loop)
for(int i = 0; i < L; i++)
{
int rowSum = 0;
for(int j = 0; j < L; j++)
{
rowSum += square_array[i][j];
}
//check each row sum to make sure it's equal to diagonal sums
if(rowSum != sum)
{
returnfalse;
}
}
//check column sums (another nested loop)
for(int i = 0; i < L; i++)
{
int colSum = 0;
for(int j = 0; j < L; j++)
{
colSum += square_array[j][i];
}
//check each column to make sure it's equal to diagonal sums
if(colSum != sum)
{
returnfalse;
}
}
//if boolean is never switched to false, return true result
returntrue;
}
int main()
{
//initialize array
int square[L][L];
//gather input from user and validate
int input, retry;
for(int i = 0; i < L; i++)
{
for(int j = 0; j < L; j++)
{
cout << "Enter a number 1-" << L * L << ": ";
cin >> input;
if(inRange(input))
{
//if input is in legal range, add to the array
square[i][j] = input;
}
else
{
//if the user entered something out of the allowed range, retry input until successful and enter correct input
while(!inRange(retry))
{
cout << endl << "You have entered an invalid integer. Please try again.\n";
cout << "Legal Integer: ";
cin >> retry;
square[i][j] = retry;
}
}
}
}
cout << endl;
//print array, just for visibility and to verify values entered
print_array(square);
//determine if array is a magic square and let the user know
if(isMagicSquare(square))
{
cout << "\nArray IS a magic square.";
}
else
{
cout << "\nArray IS NOT a magic square.";
}
//done!
return 0;
}
@itsArtem, I used to think that, but it's so common in beginner programs that I don't really care anymore. I'm aware of the potential problems, but it's nice to not have std:: all over the place.