Accelerated C++ Solution to Exercise 2-2

Exercise 2-2

Change the framing program (Exercise 2-0) so that it uses a different amount of space to separate the sides from the greeting than it uses to separate the top and bottom borders from the greeting.

Solution

The original framing program in Exercise 2-0 “hard-codes” the const int pad which forces both vertical and horizontal paddings to be identical. The question asks for a capability to assign the vertical padding and horizontal padding separately.

To do this I am going to adjust the original framing program taking into account of the followings:

  • Ask the user to provide the vertical padding, and store it as an int variable inPadY.
  • Ask the user to provide the horizontal padding, and store it as an int variable inPadX.
  • Assign the int variable inPadY to a constant integer padY.
  • Assign the int variable inPadX to a constant integer padX.
  • Replace all the (old) pad references associating with rows with padY (e.g. the constant rows and invariant r). Because the number of rows depends on vertical padding.
  • Replace all the (old) pad references associating with columns with padX (e.g. the constant cols and invariant c). Because the number of columns depends on horizontal padding.

The modified program looks like this:

#include <iostream>
#include <string>

// say what standard-library names we use
using std::cin;
using std::endl;
using std::cout;
using std::string;

int main()
{
    // ask for the person's name
    cout << "Please enter your first name: ";

    // read the name
    string name;
    cin >> name;

    // ask user to provide the vertical padding
    cout << "Please provide integer padY (vertical padding): ";
    int inPadY;
    cin >> inPadY;

    // ask user to provide the horizontal padding
    cout << "Please provide integer padX (horizontal padding): ";
    int inPadX;
    cin >> inPadX;

    // build the message that we intend to write
    const string greeting = "Hello, " + name + "!";

    // build the constant padding parameters
    const int padY = inPadY; // vertical padding
    const int padX = inPadX; // horizontal padding

    // the number of rows to write
    const int rows = ( padY * 2 ) + 3;

    // the number of columns to write
    const string::size_type cols = greeting.size() + ( padX * 2 ) + 2;

    // write a blank line to separate the output from the input
    cout << endl;

    // write 'rows' rows of output
    // invariant: we have written r rows so far
    for ( int r = 0; r != rows ; ++r )
    {
        string::size_type c = 0;

        // invariant: we have written c characters so far in the current row
        while ( c != cols)
        {
            // is it time to write the greeting?
            if ( ( r == padY+ 1 ) && ( c == padX + 1 ) )
            {
                cout << greeting;
                c += greeting.size();
            }
            else
            {
                // are we on the border?
                if (
                       ( r == 0 )         // top row
                    || ( r == rows - 1 )  // or bottom row
                    || ( c == 0 )         // or left-most column
                    || ( c == cols - 1 )  // or right-most column
                   )
                {
                    cout <<  "*";
                }
                else
                {
                    cout << " ";
                }
                ++c;
            }
        }
        cout << endl;
    }
    return 0;
}

Result

Case 1: padY = 1; padX = 10

Please enter your first name: Johnny
Please provide integer padY (vertical padding): 1
Please provide integer padX (horizontal padding): 10

************************************
*                                  *
*          Hello, Johnny!          *
*                                  *
************************************

Case 2: padY = 5; pad X = 5

Please enter your first name: Johnny
Please provide integer padY (vertical padding): 1
Please provide integer padX (horizontal padding): 10

************************************
*                                  *
*          Hello, Johnny!          *
*                                  *
************************************

Case 3: padY = 10 padX = 1

Please enter your first name: Johnny
Please provide integer padY (vertical padding): 5
Please provide integer padX (horizontal padding): 5

**************************
*                        *
*                        *
*                        *
*                        *
*                        *
*     Hello, Johnny!     *
*                        *
*                        *
*                        *
*                        *
*                        *
**************************

So far, so good!

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

Accelerated C++ Solution to Exercise 2-1

Exercise 2-1

Change the framing program (Exercise 2-0) so that it writes its greeting with no separation from the frame.

Solution

The sort of output that we are after looks like this:

</p>

<hr />

<p><em>Hello, Johnny!</em></p>

<hr />

<p>

i.e. We can just simply re-use the same program that we wrote in Exercise 2-0, except this time we change the padding of 1 (space character) to 0 (space characters). In other word, no padding round the greeting message.

For completeness sake, I have copied and pasted the code from Exercise 2-0 here. Note that this time I change the const int pad value to 0. (See line 23 below!)

</p>

<h1>include &lt;iostream&gt;</h1>

<h1>include &lt;string&gt;</h1>

<p>// say what standard-library names we use
using std::cin;
using std::endl;
using std::cout;
using std::string;</p>

<p>int main()
{
    // ask for the person's name
    cout &lt;&lt; &quot;Please enter your first name: &quot;;</p>

<pre><code>// read the name
string name;
cin &amp;gt;&amp;gt; name;

// build the message that we intend to write
const string greeting = &amp;quot;Hello, &amp;quot; + name + &amp;quot;!&amp;quot;;

// the number of blanks surrounding the greeting
const int pad = 0; // no padding this time

// the number of rows and columns to write
const int rows = ( pad * 2 ) + 3;
const string::size_type cols = greeting.size() + ( pad * 2 ) + 2;

// write a blank line to separate the output from the input
cout &amp;lt;&amp;lt; endl;

// write 'rows' rows of output
// invariant: we have written r rows so far
for ( int r = 0; r != rows ; ++r )
{
    string::size_type c = 0;

    // invariant: we have written c characters so far in the current row
    while ( c != cols)
    {
        // is it time to write the greeting?
        if ( ( r == pad + 1 ) &amp;amp;&amp;amp; ( c == pad + 1 ) )
        {
            cout &amp;lt;&amp;lt; greeting;
            c += greeting.size();
        }
        else
        {
            // are we on the border?
            if (
                   ( r == 0 )         // top row
                || ( r == rows - 1 )  // or bottom row
                || ( c == 0 )         // or left-most column
                || ( c == cols - 1 )  // or right-most column
               )
            {
                cout &amp;lt;&amp;lt;  &amp;quot;*&amp;quot;;
            }
            else
            {
                cout &amp;lt;&amp;lt; &amp;quot; &amp;quot;;
            }
            ++c;
        }
    }
    cout &amp;lt;&amp;lt; endl;
}
return 0;
</code></pre>

<p>}</p>

<p>

Result

The program compiles successfully and produce the following output in the standard console output window.

Please enter your first name: Johnny</p>

<hr />

<p><em>Hello, Johnny!</em></p>

<hr />

<p>Process returned 0 (0x0)   execution time : 2.905 s
Press any key to continue.

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

Accelerated C++ Solution to Exercise 2-0

Exercise 2-0

Compile and run the program presented in this chapter.

Solution

Note: The comprehensive program and explanation are documented very clearly in chapter 2 of the book. Refer to the chapter for details.

This is what the program does:

  • Ask user for his first name.
  • Display a framed greeting message that looks like this:
******************
*                *
* Hello, Johnny! *
*                *
******************

Specification as follows:

  • The greeting message is in the middle of the frame.
  • We add a padding of 1 (space character) all the way around the greeting message.
  • We display the frame with asterisk (*).

This is the full code as described in the chapter. I have added some extra brackets for clarity purpose.

#include <iostream>
#include <string>

// say what standard-library names we use
using std::cin;
using std::endl;
using std::cout;
using std::string;

int main()
{
    // ask for the person's name
    cout << "Please enter your first name: ";

    // read the name
    string name;
    cin >> name;

    // build the message that we intend to write
    const string greeting = "Hello, " + name + "!";

    // the number of blanks surrounding the greeting
    const int pad = 1;

    // the number of rows and columns to write
    const int rows = ( pad * 2 ) + 3;
    const string::size_type cols = greeting.size() + ( pad * 2 ) + 2;

    // write a blank line to separate the output from the input
    cout << endl;

    // write 'rows' rows of output
    // invariant: we have written r rows so far
    for ( int r = 0; r != rows ; ++r )
    {
        string::size_type c = 0;

        // invariant: we have written c characters so far in the current row
        while ( c != cols)
        {
            // is it time to write the greeting?
            if ( ( r == pad + 1 ) && ( c == pad + 1 ) )
            {
                cout << greeting;
                c += greeting.size();
            }
            else
            {
                // are we on the border?
                if (
                       ( r == 0 )         // top row
                    || ( r == rows - 1 )  // or bottom row
                    || ( c == 0 )         // or left-most column
                    || ( c == cols - 1 )  // or right-most column
                   )
                {
                    cout <<  "*";
                }
                else
                {
                    cout << " ";
                }
                ++c;
            }
        }
        cout << endl;
    }
    return 0;
}

Result

The program compiles successfully and produce the following output in the standard console output window.

Please enter your first name: Johnny

******************
*                *
* Hello, Johnny! *
*                *
******************

Process returned 0 (0x0)   execution time : 2.699 s
Press any key to continue.

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000