I'm trying to develop a program that takes input from the user (an odd number between 3 and 25) and then uses that to display the magic square for that number. This is what I have so far:
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;
const int MAXBOARD = 25;
const bool DEBUG = true;
void displayHeaders ( );
int getInput ( );
void initializeBoard ( int magic [][MAXBOARD] );
void displayMagic ( int magic[ ][MAXBOARD], int ssize );
void makeMagic (int magic[ ][MAXBOARD], int ssize );
int main()
{
int MSsize;
int magic [MAXBOARD][MAXBOARD];
displayHeaders ();
MSsize = getInput();
initializeBoard (magic);
makeMagic (magic, MSsize);
displayMagic (magic, MSsize);
cout << endl << " " << endl<< endl;
system("PAUSE");
return 0;
}
void displayHeaders ( )
{
cout <<endl<<endl<< endl<< ""<<endl<< "Program";
cout << endl <<"Magic Square Generator"<< endl << endl;
return;
}
int getInput ( )
{
int numInput;
cout << endl<< "Please enter an odd number between 3 and 25! ";
while(1)
{
cin >> numInput;
if( numInput >= 3 && numInput <= 25 && numInput%2==1)
{
return numInput;
}
else
{
cout<<"Invalid Input! Please try again with an ODD number between 3 and 25: ";
}
}
return numInput;
}
void initializeBoard ( int magic [][MAXBOARD] )
{
if (DEBUG) cout << endl<< endl<<"Into initializeBoard" << endl;
for ( int row= 0; row < MAXBOARD; row ++)
for (int col= 0; col< MAXBOARD; col ++ )
magic[row][col] = 0;
}
void displayMagic ( int magic[ ][MAXBOARD], int ssize )
{
if (DEBUG) cout << endl<< endl<< "In displayMagic, Your Magic Square is:" << endl;
for ( int row= 0; row < ssize; row ++)
{
for (int col= 0; col< ssize; col ++ )
cout << "\t" << magic[row][col];
cout << endl;
}
}
void makeMagic (int magic[][MAXBOARD], int ssize )
{
if (DEBUG) cout << endl <<"Into makeMagic"<< endl << endl;
}
The loop works fine, so that much is done, but I just do not understand how to calculate the magic square and then how to print it. I would appreciate any help I can get with understanding how best to write this program. :)