:?: I work on the program to function a Magic square, below as my working program, but I don't know how to print out the magic square using print function
any one can help?

#include<iostream>
#include<iomanip>
using namespace std;
const int n = 30;
int MagicSquareConstruct(int MagicSquare[n][n]);
int main()
{
   int entrySize;
   int MagicSquare[n][n];
   cout<< "Please enter an integer n: ";
   cin>>entrySize;
   cout<< MagicSquareConstruct(MagicSquare) << endl;
}

int MagicSquareConstruct(int MagicSquare[n][n])
{
   int newRow,
   newCol;

   // Set the indices for the middle of the bottom i
   int i =0 ; 
   int j= n / 2;

   // Fill each element of the array using the magic array
   for ( int value = 1; value <= n*n; value++ )
   {
      MagicSquare[i][j] = value;
      // Find the next cell, wrapping around if necessary.
      newRow = (i + 1) % n; 
      newCol = (j + 1) % n;
      // If the cell is empty, remember those indices for the
      // next assignment.
      if ( MagicSquare[newRow][newCol] == 0 )
      {
         i = newRow;
         j = newCol;
      }
      else
      {
         // The cell was full. Use the cell above the previous one.
         i = (i - 1 + n) % n;
      }
   }
}

by "Print function" I guess you mean the method to output to the screen or console window? the typical way to output the contents of a 2D array involve a nested loop, eg,

for(int x(0); x!=total_rows; ++x)
{
    for(int y(0); x!=total_columns; ++y)
        std::cout << arrays[x][y];
    std::cout << endl;
}

by "Print function" I guess you mean the method to output to the screen or console window? the typical way to output the contents of a 2D array involve a nested loop, eg,

for(int x(0); x!=total_rows; ++x)
{
    for(int y(0); x!=total_columns; ++y)
        std::cout << arrays[x][y];
    std::cout << endl;
}

can five me more detail how to insert the print function into my coding? I try to put in but the loop become the inifitive loop

What print function are you referring to specifically? do you mean the C function printf()? You shouldn't be using printf with C++ - that's what cout is for.

This article has been dead for over six months. Start a new discussion instead.