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.

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

The output of each program is as follows:

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.

The output of the program is as follows:

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.

The output of the program is as follows:

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.

The output of the program is as follows:

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.

The output of the program is as follows:

Exercise 2-6

What does the following code do?

expand/collapse icon Solution

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

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.

The output of the program is as follows:

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.

The output of the program is as follows:

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.

The output of the program is as follows:

Exercise 2-10

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

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.

Share

9 Comments

  1. Exercise 2-1:
    I for one understood the exercise as I was asked only to not separate the statement from the frame. Not to remove the blanks altogether. Because if you removed the blanks there would be no reason for { // are we on the border?
    if (r == 0 || r == rows – 1 ||
    c == 0 || c == cols – 1)
    cout << "*";
    else
    cout < the original being
    –> const string::size_type cols = greeting.size() +pad*2 + 2;
    and
    —>if ( r == pad + 1 && c == 1 ) –> starting code was –> if ( r == pad + 1 && c == pad + 1 )
    We check if we have written the first ‘*’ and if we are right in the middle ready to write the greeting..

    This was the way I understood the exercise. It might not be correct but then there’s a redundant amount of code if we aren’t writing any empty lines.

  2. I used a different method for the triangle of exercise 2-5, I used(for) instead of (while). I don’t know if you would consider this less or more complicated, however, I wanted to share with anyone would like to see.

    Note: I personnaly like to ask the user to input the size even though it wasn’t asked in the exercise, if any was wondering why I added that

    //ask how big the triagle shall be

    cout << "how high do you want the triangle to be" <> rows;
    cout << endl;

    //Determine number of colums

    const int cols = rows * 2 – 1;

    // Start drawing the tringle
    //invariant : we have r rows so far

    for (int r = 0; r != rows ; ++r){

    //invariant: we have c columns so far
    for (int c = 0; c != cols; ++c){
    if (r == rows -1) // This if draws the base
    cout << "*";
    else
    if (c == (cols/2 – r) || c == (cols/2 + r) ) //could also be c == rows -1 +r and c == rows – 1 + r
    cout << "*";
    else
    cout << " ";
    }

    cout << endl;

    }

  3. Hi there. This page is really a great help. I’m doing my work from the Accelerated C++ book and following up on the page. It’s proving to be a great advantage toward my studies.
    I’m experiencing the same issue as “Snoopz”. When I enter a name, the only output I get is a row of “*”. I’ve even tried copying the solution’s code just to check, but I’ve gotten the same result

Leave a Reply

Your email address will not be published. Required fields are marked *

*