For an assignment, we are meant to be able to create a magic square when passed an odd number for the order. However, I can't get my print function to properly display the values that have been set to the array (in the default case, all values are set to zero). Some of the values display accurately while others are showing garbage values.
static const int NUM_ROWS{15}, NUM_COLUMNS{15};
typedef int TwoDimArray[NUM_ROWS][NUM_COLUMNS];
TwoDimArray** buildMagicSquare1(int order) {
int** table{};
table = new int *[order];
for (int row{0}; row < order; ++row)
{
table[row] = new int[order];
for (int col{0}; col < order; ++col)
{
table[row][col] = 0;
}
}
// table is now a TwoDimArray initialized to all zeros.
return reinterpret_cast<TwoDimArray **>(table);
}
void printTable(int rows, int cols, TwoDimArray aTable){
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
std::cout << std::setw(5) << aTable[i][j];
std::cout << std::endl;
}
}
int main()
{
printTable(3, 3, **buildMagicSquare1(3));
}