/* Square Display - This program asks the user for a positive integer no
greater than 15. The program then displays a square on the screen using
the character 'X'. The number entered by the user will be the length of
each side of the square.
Example:
If the user enters 5, the program displays:
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
If the user enters 8, the program displays:
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX */
#include "Utility.h"
int main()
{
/* Constant: Minimum number of 'X' characters,
Maximum number of 'X' characters */
const int CHARACTERS_MIN = 2,
CHARACTERS_MAX = 15;
/* Variables: Pattern character */
char pattern = 'X';
/* Variables: Pattern length, Number of rows (counter variable),
Number of columns (counter variable) */
int patternLength = 0,
numRows,
numColumns;
/* Display: Information */
cout << "\t\tSQUARE DISPLAY\n\n"
<< "Please enter the number of squares for the pattern to\n"
<< "display. ( " << CHARACTERS_MIN
<< " is the lowest, " << CHARACTERS_MAX << " the highest).\n"
<< "Once done, press enter, and enjoy the created pattern.\n\n"; cout << "Number of squares: ";
cin >> patternLength;
/* While loop::Input Validation: If the number entered by the user
is less than CHARACTERS_MIN, or greater than CHARACTERS_MAX, this
statement will be executed */
while (patternLength < CHARACTERS_MIN || patternLength > CHARACTERS_MAX)
{
/* Display: Error message */
cout << "\nSorry, it seems that the number entered by you was\n"
<< "either below " << CHARACTERS_MIN
<< " or greater than " << CHARACTERS_MAX << ". Please try again.\n";
/* Ask again */
cout << "Number of squares: ";
cin >> patternLength;
}
cout << endl;
/* For loop: As long as number of rows is smaller than pattern length,
this loop will iterate */
for (numRows = 0; numRows < patternLength; numRows++)
{
/* Nested For loop: As long as the number of columns is smaller
than pattern length, this loop will iterate */
for (numColumns = 0; numColumns < patternLength; numColumns++)
{
/* Display: pattern */
cout << pattern;
}
cout << endl;
}
pauseSystem();
return 0;
}
Tuesday, December 27, 2016
Programming Challenge 5.22 - Square Display
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment