Wednesday, December 28, 2016

Programming Challenge 5.23 - Pattern Displays

/* Pattern Displays - This program uses a loop to display Pattern A below,
   followed by another loop that displays Pattern B

   -----------------------------------------------------------------
   Pattern A                        Pattern B
   -----------------------------------------------------------------
   +                                     ++++++++++
   ++                                   +++++++++
   +++                                 +++++++
   ++++                               ++++++
   +++++                             +++++
   +++++++                         ++++
   ++++++++                       +++
   +++++++++                     ++
   ++++++++++                   +
----------------------------------------------------------------- */

#include "Utility.h"

int main()
{
    /* Constants: Minimum number of pattern symbols, Maximum number
    of pattern symbols */
    const int PATTERN_SYM_MIN = 1,
              PATTERN_SYM_MAX = 10;

    /* Variables: Pattern rows (counter variable), Pattern columns A
       (counter variable), Pattern columns B (counter variable),
       Window width (counter variable) */
    int patternRows = 0,
        columnsPatternA = 0,
        columnsPatternB = 0,
        windowWidth = 0;

    /* Display: Header - Pattern A - PATTERN B */
    cout << "-----------------------------------------\n";
    cout << "PATTERN A" << "         PATTERN B"
        << "\n-----------------------------------------\n";

    /* For loop: Pattern rows gets PATTERN_SYM_MAX, as long patternRows
       is greater than or equal to PATTERN_SYM_MIN, this loop iterates
       and decrements pattern rows */
    for (patternRows = PATTERN_SYM_MAX; patternRows >= PATTERN_SYM_MIN;
        patternRows--)
    {
        /* Nested For loop: Columns pattern A gets PATTERN_SYM_MAX, as
           long as columns pattern A is greater than or equal to pattern
           rows == PATTERN_SYM_MAX (at first), this loop decrements once,
           adding 1 +, decrementing to PATTERN_SYM_MAX - 1, decrements twice,
           getting PATTERN_SYM_MAX during every iteration, until pattern rows
           reaches PATTERN_SYM_MIN, by which time 10 + are displayed */
        for (columnsPatternA = PATTERN_SYM_MAX; columnsPatternA >= patternRows;
            columnsPatternA--)
        {
            cout << "+ ";
        }

        /* Nested For loop: Window width gets PATTERN_SYM_MIN, as long as
           window width is smaller than or equal to PATTERN_SYM_MAX +
           patternRows (ex. 20), this loop iterates, adding blank spaces -
           dividing up pattern A and pattern B horizontally */
        for (windowWidth = PATTERN_SYM_MIN;
            windowWidth <= (PATTERN_SYM_MAX + patternRows);
            windowWidth++)
        {
            cout << " ";
        }

        /* Nested For loop: Columns pattern B gets PATTERN_SYM_MIN, as long
           as columns pattern B is smaller than or equal to pattern Rows ==
           PATTERN_SYM_MAX (at first), this loop iterates, and increments
           PATTERN_SYM_MIN (ex. displaying 10 +'s), then gets PATTERN_SYM_MAX
           again until pattern rows == PATTERN_SYM_MIN, by which time only one
           + is shown at the bottom */
        for (columnsPatternB = PATTERN_SYM_MIN; columnsPatternB <= patternRows;
            columnsPatternB++)
        {
            cout << "+ ";
        }

        cout << endl;
    }

    /* Display table footer */
    cout << "-----------------------------------------\n";

    pauseSystem();
    return 0;
}

No comments:

Post a Comment