/* square1.cpp - Beware: this solution, though interesting, is not the 
	clearest, or most efficient way to solve this problem.
   Problem Statement:
	Write a program that prompts the user to 
	enter a positive integer, say n, and then prints out a 
	square centered on the line (which you can assume to
	be 60 columns wide).
	If the number entered by the user is greater than 60, then 
	the sides will not be displayed, but 60 characters of the 
	top and the bottom will be printed out. If n is greater than 
	74, do as if it were 74.
   Analysis:
	The only input is n.
	The output is the printout of the square.
	Consired the case where 0<n<75. The other cases are easily
	handled.
	Say MAX_LINE is 60. We count the rows from 0 to n-1
	and the columns from 0 to (MAX_LINE+n)/2-1.
	o The left border is in column (MAX_LINE-n)/2
	o The right border is in column (MAX_LINE+n)/2-1
	o The top and bottom rows (excluding leftmost and rightmost
	  character) are from column 1+(MAX_LINE-n)/2 to
	  column (MAX_LINE+n)/2-2
   Design (Algorithm):
	Prompt the user and read the value of n.
	Take care of the cases n<1 and n>74.
	For row from 0 to n-1
	    For column from 0 to (MAX_LINE-n)/2
		If the current position is on the border of the square
		   print out 'X'
		else
		   print out ' '
	    Print out a Carriage Return
   Testing:
	Run program with the following values of n: 
		-7,0,1,2,20,MAX_LINE-1,MAX_LINE,MAX_LINE+1,74,75,100
 */

#include <iostream>

const int MAX_LINE = 60;
const int MAX_ROWS = 74;

/* It returns true iff the specified row and column are
   on the boundary of a centered square of side n */
bool onSquareBoundary(unsigned int row, unsigned int col, int n)
{
   return ((((row == 0) || (row == (n-1)))
	        && (col > (MAX_LINE-n)/2) 
	        && (col < (MAX_LINE+n)/2-1))
	      || (col == (MAX_LINE-n)/2)
	      || (col == (MAX_LINE+n)/2-1));
}


/* Assuming 0 < n < MAX_ROWS, print out a hollow square
   of size n.
 */
void drawSquare(int n)
{
   for (int row = 0; row < n; ++row) {
       for (int col = 0; col < (MAX_LINE+n)/2; ++col) {
	  if (onSquareBoundary(row, col, n))
	      cout << 'X';
	  else
	      cout << ' ';
       }
       cout << endl;
   }
}

void main ()
{
   int side;

   cout << "Enter value of side of square: ";
   cin >> side;
   if (side < 1) 
      cout << "Sorry, the side should be at least 1\n";
   else {
      if (side > MAX_LINE)
	 side = MAX_LINE;
      drawSquare(side);
   }
}