(updated )

Chapter 2 – Looping and Counting

Solutions to exercises in Chapter 2 of Accelerated C++, “Looping and counting.”

Exercise 2-1

Change the framing program so that it writes its greeting with no separation from the frame.

expand/collapse icon Solution

The simplest way to accomplish this is to change the value of the constant pad to 0.

#include <iostream>
#include <string>

// say what standard-library names we use
using std::cin;
using std::cout;
using std::endl;
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 = 0;

   // 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 || r == rows - 1 ||
                c == 0 || c == cols - 1)
               cout << "*";
            else
               cout << " ";
            ++c;
         }
      }

      cout << endl;
   }

   return 0;
}

An alternative is to remove the pad constant from the program.

#include <iostream>
#include <string>

// say what standard-library names we use
using std::cin;
using std::cout;
using std::endl;
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 rows and columns to write
   const int rows = 3;
   const string::size_type cols = greeting.size() + 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 == 1 && c == 1) {
            cout << greeting;
            c += greeting.size();
         } else {

            // are we on the border?
            if (r == 0 || r == rows - 1 ||
                c == 0 || c == cols - 1)
               cout << "*";
            else
               cout << " ";
            ++c;
         }
      }

      cout << endl;
   }

   return 0;
}

The output of each program is as follows:

Please enter your first name: Paul

**************
*Hello, Paul!*
**************

Exercise 2-2

Change the framing program 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.

expand/collapse icon Solution

In the program below the pad constant has been removed, and in its place are now two constants, vertical_pad and horizontal_pad. The vertical_pad constant is used to calculate the number of rows to write, and the horizontal_pad constant is used to calculate the width of each line written.

#include <iostream>
#include <string>

// say what standard-library names we use
using std::cin;
using std::cout;
using std::endl;
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 above and below the greeting
   const int vertical_pad = 4;

   // the number of blanks to the left and right of the greeting
   const int horizontal_pad = 2;

   // the number of rows and columns to write
   const int rows = vertical_pad * 2 + 3;
   const string::size_type cols = greeting.size() + horizontal_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 == vertical_pad + 1 && c == horizontal_pad + 1) {
            cout << greeting;
            c += greeting.size();
         } else {

            // are we on the border?
            if (r == 0 || r == rows - 1 ||
                c == 0 || c == cols - 1)
               cout << "*";
            else
               cout << " ";
            ++c;
         }
      }

      cout << endl;
   }

   return 0;
}

The output of the program is as follows:

Please enter your first name: Paul

******************
*                *
*                *
*                *
*                *
*  Hello, Paul!  *
*                *
*                *
*                *
*                *
******************

Exercise 2-3

Rewrite the framing program to ask the user to supply the amount of spacing to leave between the frame and the greeting.

expand/collapse icon Solution

This solution is similar to the first solution provided for exercise 2-1, except that it
asks the user to input the amount of padding to display inside the frame. Note that the pad variable is no
longer constant since it must be determined when the program runs.

#include <iostream>
#include <string>

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

int main()
{
   // ask for the number of blanks to surround person's name.
   cout << "Please enter number of blanks for frame padding: ";

   // read the number of blanks to surround the greeting
   int pad;
   cin >> pad;

   // 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 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 || r == rows - 1 ||
                c == 0 || c == cols - 1)
               cout << "*";
            else
               cout << " ";
            ++c;
         }
      }

      cout << endl;
   }

   return 0;
}

The output of the program is as follows:

Please enter number of blanks for frame padding: 3
Please enter your first name: Paul

********************
*                  *
*                  *
*                  *
*   Hello, Paul!   *
*                  *
*                  *
*                  *
********************

Exercise 2-4

The framing program writes the mostly blank lines that separate the borders from the greeting one character at a time. Change the program so that it writes all the spaces needed in a single output expression.

expand/collapse icon Solution

This solution creates a string constant named padding that consists of the appropriate number of spaces to output between the border character and the greeting. It also adds a filler constant that consists of the number of spaces between the borders on lines above and below the greeting.

In the output loop, after determining that spaces should be output, the program tests to determine if the current row is a padding row or the greeting row, and outputs the appropriate string constant.

#include <iostream>
#include <string>

// say what standard-library names we use
using std::cin;
using std::cout;
using std::endl;
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 = 5;

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

   // string of spaces equal in size to the padding between
   // the border and the greeting
   const string padding(pad, ' ');

   // string of spaces to fill the empty area between borders
   // above and below the greeting
   const string filler(greeting.size() + pad * 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 || r == rows - 1 ||
                c == 0 || c == cols - 1) {
               cout << "*";
               ++c;
            } else {

               // are we on the greeting line?
               if (r == pad + 1) {

                  // output the padding
                  cout << padding;
                  c += padding.size();
               } else {
                  
                  // output empty space between borders
                  cout << filler;
                  c += filler.size();
               }
            }
         }
      }

      cout << endl;
   }

   return 0;
}

The output of the program is as follows:

Please enter your first name: Paul

************************
*                      *
*                      *
*                      *
*                      *
*                      *
*     Hello, Paul!     *
*                      *
*                      *
*                      *
*                      *
*                      *
************************

Exercise 2-5

Write a set of “*” characters so that they form a square, a rectangle, and a triangle.

expand/collapse icon Solution

This solution outputs each of the three figures to be height rows. Changing this constant will change the size of each shape. Each shape is built until height rows have been printed.

#include <iostream>
#include <string>

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

int main()
{
   // this constant is the height of each figure
   const int height = 5;

   // these variables are used to track the row and
   // column count while drawing each figure
   int row;
   int col;

   
   // draw square
   row = 0;
   col = 0;

   // draw top line
   while (col < height) {
      cout << '*';
      ++col;
   }

   cout << endl;
   ++row;

   // draw middle lines
   while (row < height - 1) {

      col = 0;

      while (col < height) {

         ++col;
         
         if (col == 1)
            cout << '*';
         else {
            if (col == height)
               cout << '*';
            else
               cout << ' ';
         }

      }

      cout << endl;
      ++row;
   }

   // draw bottom line
   col = 0;

   while (col < height) {
      cout << '*';
      ++col;
   }

   // new line after figure
   cout << endl;

   // blank line between figures
   cout << endl;

   // draw rectangle
   row = 0;
   col = 0;

   // draw top line
   while (col < height * 2) {
      cout << '*';
      ++col;
   }

   cout << endl;
   ++row;

   // draw middle lines
   while (row < height - 1) {

      col = 0;

      while (col <= height * 2) {

         ++col;

         if (col == 1)
            cout << '*';
         else {
            if (col == height * 2)
               cout << '*';
            else
               cout << ' ';
         }
      }

      cout << endl;
      ++row;
   }

   // draw bottom line
   col = 0;

   while (col < height * 2) {
      cout << '*';
      ++col;
   }

   // new line after figure
   cout << endl;

   // blank line between figures
   cout << endl;

   // draw triangle
   row = 0;

   // draw rows above base
   while (row < height - 1)
   {
      col = 0;

      while (col < height + row) {

         ++col;

         if (col == height - row)
            cout << '*';
         else {

            if (col == height + row)
               cout << '*';
            else
               cout << ' ';
         }
      }

      cout << endl;
      ++row;
   }

   // draw the base
   col = 0;

   while (col < height * 2 - 1) {
      cout << '*';
      ++col;
   }

   // new line after figure
   cout << endl;

   return 0;
}

The output of the program is as follows:

*****
*   *
*   *
*   *
*****

**********
*        *
*        *
*        *
**********

    *
   * *
  *   *
 *     *
*********

Exercise 2-6

What does the following code do?

int i = 0;
while (i < 10) {
    i += 1;
    std::cout << i << std::endl;
}

expand/collapse icon Solution

This code counts from 1 to 10, displaying each number on a new line as it counts.

1
2
3
4
5
6
7
8
9
10

Exercise 2-7

Write a program to count down from 10 to -5.

expand/collapse icon Solution

The loop variable starts at 11, which is outside the beginning of the range. The program decrements the loop variable until it is equal to the end of the range.

#include <iostream>
using std::cout;
using std::endl;

int main()
{
   int i = 11;
   while (i > -5) {
      --i;
      cout << i << endl;
   }
}

The output of the program is as follows:

10
9
8
7
6
5
4
3
2
1
0
-1
-2
-3
-4
-5

Exercise 2-8

Write a program to generate the product of the numbers in the range [1,10).

expand/collapse icon Solution

The half-open range [1,10) includes the numbers 1 through 9. The program loops until all the numbers in the range have been multiplied. The variable product contains the product of the range when the loop exits.

#include <iostream>
using std::cout;
using std::endl;

int main()
{
   int product = 1;
   int i = 0;
   
   while (i < 9) {
      ++i;
      product *= i;
   }
   cout << product << endl;
}

The output of the program is as follows:

362880

Exercise 2-9

Write a program that asks the user to enter two numbers and tells the user which number is larger than the other.

expand/collapse icon Solution

The program asks the user for two numbers, then tests if the first number is less than the second number. If the first number is the larger number, the program reports that fact; otherwise, it reports that the second number is the larger number.

This program does not handle the situation where the two numbers are equal.

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
   int first;
   cout << "Enter the first number: ";
   cin >> first;
   cout << endl;
   
   int second;
   cout << "Enter the second number: ";
   cin >> second;
   cout << endl;

   if (first > second)
   {
      cout << "The number " << first << " is the larger number." << endl;
   }
   else if (second > first)
   {
      cout << "The number " << second << " is the larger number." << endl;
   }
   else
   {
      cout << "The numbers are equal." << endl;
   }
}

The output of the program is as follows:

Enter the first number: 3

Enter the second number: 5

The number 5 is the larger number.

Exercise 2-10

Explain each of the uses of std:: in the following program:

int main()
{
   int k = 0;
   while (k != n) {          // invariant: we have written k asterisks so far
      using std::cout;
      cout << "*";
      ++k;
   }
   std::cout << std::endl;   // std:: is required here
   return 0;
}

expand/collapse icon Solution

The first use of std:: is in the using-declaration. The name cout is declared to mean std::cout. The second time std:: appears is to make use of the std::cout stream to output a blank line. Because the using-declaration does not apply at this line, the std:: namespace must be used explicitly. The last use of std:: is to output a new line with the std::endl manipulator.