/* printstairs.cpp - Write and test a function that, given 
 *          a positive integer n, will print out n rows forming
 *          a stair. For example, if n is 5, it will print out
 *              X
 *             XX
 *            XXX
 *           XXXX
 *          XXXXX
 *          where we assume that the leftmost X of the bottom 
 *          line is in column 1. [Inspired from a problem in
 *          "Programming for Everyone in Java" by Brinch Hansen.]
 */

#include <iostream>

/* print rows with one 'X', two 'X's, .. n 'X's
   so that they form a stair as required by the problem.
   We assume that n is a positive integer and that the
   stair lines will fit in a line.
 */
void printStairs(int n)
{
   for (int k = 0; k < n; ++k) {
     // print a step
        // print (n-k-1) leading spaces
	for (int col = 0; col < n-k-1; ++col)
	    cout << ' ';
        // print the (k+1) Xs
	for (int col = 0; col < k+1; ++col)
	    cout << 'X';
	cout << endl;
   }
}

void main()
{
	int n;

	cout << "Enter a positive n: ";
	cin >> n;
	if (n<1) // So that we print at least one line.
	   n = 1;
	else if (n>60) // So that we avoid wraparound.
	   n = 60;
	printStairs(n);
}
