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.
Solution
The simplest way to accomplish this is to change the value of the constant pad
to 0
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#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:
1 2 3 4 5 |
Please enter your first name: <b>Paul</b> ************** *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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Please enter your first name: <b>Paul</b> ****************** * * * * * * * * * 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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#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:
1 2 3 4 5 6 7 8 9 10 11 12 |
Please enter number of blanks for frame padding: <b>3</b> Please enter your first name: <b>Paul</b> ******************** * * * * * * * 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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Please enter your first name: <b>Paul</b> ************************ * * * * * * * * * * * Hello, Paul! * * * * * * * * * * * ************************ |
Exercise 2-5
Write a set of “*” characters so that they form a square, a rectangle, and a triangle.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
***** * * * * * * ***** ********** * * * * * * ********** * * * * * * * ********* |
Exercise 2-6
What does the following code do?
1 2 3 4 5 |
int i = 0; while (i < 10) { i += 1; std::cout << i << std::endl; } |
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 |
1 2 3 4 5 6 7 8 9 10 |
Exercise 2-7
Write a program to count down from 10
to -5
.
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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)
.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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:
1 |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#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:
1 2 3 4 5 |
Enter the first number: <b>3</b> Enter the second number: <b>5</b> The number 5 is the larger number. |
Exercise 2-10
Explain each of the uses of std::
in the following program:
1 2 3 4 5 6 7 8 9 10 11 |
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; } |
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.
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.
Somehow I cannot get the full code to show :(
I’ll edit your comment a little later on when I get time. I’m awfully swamped at the moment. :(
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;
}
hmm, its seems to no properly show the code I wrote.
first part should be
//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;
it did it again, I give up, sry for the spam
Thanks for the solutions! Please add the remaining chapters. Great work!
You are so good! Thank you !
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