Section 1

<- View full list of my projects

As I work my way through the C++ Primer book (5th Edition, for C++11, by Lippman, Lajoie and Moo) I thought I would post my answers (or answers that worked for me, if I got stuck and had to ask someone) to the exercises as a resource for anybody who may get stuck.  I hope they are useful!  If you need an explanation (or think I made a mistake), I’m happy to discuss, just use the comments section at the bottom.

Health and Safety Warning: There’s no guarantee that the below is correct. I checked most of the content, but typos do get through. If there is something you disagree with, please use either the comment section below or contact me.


Section 1

Exercise 1.1

Read the docs 🙂

Exercise 1.2

<br />int main() {<br />    return -1;<br />}<br />

In Xcode, nothing happens in the compiler, but check on your own IDE / compiler output.

Exercise 1.3

<br />int main() {<br />    std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl;<br />    return 0;<br />}<br />

Exercise 1.4

<br />int main() {<br />    std::cout &lt;&lt; "Enter two ints" &lt;&lt; std::endl;     int int1 = 0, int2 = 0;     std::cin &gt;&gt; int1 &gt;&gt; int2;<br />    std::cout &lt;&lt; int1 &lt;&lt; "*" &lt;&lt; int2 &lt;&lt; "=" &lt;&lt; int1*int2;<br />}<br />

Exercise 1.5

<br />int main() {<br />    std::cout &lt;&lt; "Enter two ints" &lt;&lt; std::endl;     int int1 = 0, int2 = 0;     std::cin &gt;&gt; int1 &gt;&gt; int2;<br />    std::cout &lt;&lt; int1;<br />    std::cout &lt;&lt; "*";<br />    std::cout&lt;&lt; int2;<br />    std::cout&lt;&lt; "=";<br />    std::cout&lt;&lt; int1*int2;<br />}<br />

Exercise 1.6

Not legal – has end of line indicator (;) in the middle of what should be a single line instruction.

Exercise 1.7

<br />int main() {<br />    /*<br />     /*<br />     this won't be executed<br />     */<br />     but this will, and we don't want it to<br />    */          //and this end of comment marker is also going to cause a problem<br />    std::cout &lt;&lt; "Hello, World", &lt;&lt; std::endl;<br />}<br />

Exercise 1.8

The third and fourth won’t compile correctly.

Exercise 1.9

<br />int main() {<br />    int sum = 0, val = 50;<br />    while (val &lt;= 100) {<br />        sum += val;<br />        ++val;<br />    }<br />    std::cout &lt;&lt; sum &lt;&lt; std::endl;<br />}<br />

Exercise 1.10

<br />int main() {<br />    int myInt = 10;<br />    while (myInt &gt;= 0) {<br />        std::cout &lt;&lt; myInt &lt;&lt; std::endl;<br />        --myInt;<br />    }<br />}<br />

Exercise 1.11

<br />int main() {<br />    std::cout &lt;&lt; "Enter the smaller int, then the larger int" &lt;&lt; std::endl;     int int1 = 0, int2 = 0;     std::cin &gt;&gt; int1 &gt;&gt; int2;<br />    ++int1;<br />    while (int1 &lt; int2) {<br />        std::cout &lt;&lt; int1 &lt;&lt; std::endl;<br />        ++int1;<br />    }<br />}<br />

Exercise 1.12

Sums the numbers from -100 to 100, resulting in 0.

Exercise 1.13

Repeat for Ex 1.9 Using For:

<br />int main() {<br />    int sum = 0;<br />    for (int i = 50; i&lt;=100; i++) {<br />        sum += i;<br />    }<br />    std::cout &lt;&lt; sum &lt;&lt; std::endl;<br />}<br />

Repeat for Ex 1.10 Using For:

<br />int main() {<br />    for (int i = 10; i &gt;= 0; i--) {<br />        std::cout &lt;&lt; i &lt;&lt; std::endl;<br />    }<br />}<br />

Repeat for Ex 1.11 Using For:

<br />int main() {<br />    std::cout &lt;&lt; "Enter the smaller int, then the larger int" &lt;&lt; std::endl;     int int1 = 0, int2 = 0;     std::cin &gt;&gt; int1 &gt;&gt; int2;<br />    ++int1;<br />    for (; int1 &lt; int2; int1++) {<br />        std::cout &lt;&lt; int1 &lt;&lt; std::endl;<br />    }<br />}<br />

Exercise 1.14

When there is any kind of count or iteration going on, a For is more useful – especially when you know how many times you want to loop through the iteration. However, a While is useful when the condition is not based around one of these factors, or when it is based on a different variable (i.e. a boolean).

Exercise 1.15

See the examples in the book on page 16.

Exercise 1.16

It’s worth remembering here that when testing an istream, true is returned if no errors were encountered (such as unexpected variable types) and false is returned if an error occurred or and end of file is detected (control + d on a Mac or Unix).  Remember that the condition inside the While brackets also reads the data on the istream into val as well as testing the condition.

<br />int main() {<br />    int sum = 0, val = 0;<br />    while (std::cin &gt;&gt; val) {<br />        sum += val;<br />    }<br />    std::cout &lt;&lt; sum &lt;&lt; std::endl;<br />}<br />

Exercise 1.17

The ‘else’ block will never be run. Lets say that the int n is entered x times, the output will be ‘n occurs x times’ – i.e. the program will execute correctly.
If there are no duplicate values, the program will also execute correctly, and the output will show that all the inputs occurred once.

Exercise 1.18

There’s not much I can write here.

Exercise 1.19

<br />int main() {<br />    std::cout &lt;&lt; "Enter two ints:" &lt;&lt; std::endl;     int int1 = 0, int2 = 0, tempInt = 0;     std::cin &gt;&gt; int1 &gt;&gt; int2;<br />    if (int1 &gt; int2) {<br />        tempInt = int1;<br />        int1 = int2;<br />        int2 = tempInt;<br />    }<br />    ++int1;<br />    for (; int1 &lt; int2; int1++) {<br />        std::cout &lt;&lt; int1 &lt;&lt; std::endl;<br />    }<br />}<br />

Exercise 1.20

<br />int main() {<br />    Sales_item item1, item2;<br />    std::cout &lt;&lt; "Enter info for item1 followed by info for item2 (3 entires each - isbn, number sold and price)" &lt;&lt; std::endl;     std::cin &gt;&gt; item1 &gt;&gt; item2;<br />    std::cout &lt;&lt; "output item1: "&lt;&lt; item1 &lt;&lt; " and output item2: "&lt;&lt; item2 &lt;&lt; std::endl;<br />}<br />

Exercise 1.21

<br />int main() {<br />    Sales_item item1, item2;<br />    std::cout &lt;&lt; "Enter info for item1 followed by info for item2 (3 entires each - isbn, number sold and price)" &lt;&lt; std::endl;     std::cin &gt;&gt; item1 &gt;&gt; item2;<br />    std::cout &lt;&lt; "output of sum: "&lt;&lt; item1+item2 &lt;&lt; std::endl;<br />}<br />

Exercise 1.22

<br />int main() {<br />    Sales_item sumItem, tempItem;<br />    std::cout &lt;&lt; "Enter info for the first transaction: " &lt;&lt; std::endl;     std::cin &gt;&gt; sumItem;<br />    std::cout &lt;&lt; "If you want to enter another transaction, do so: " &lt;&lt; std::endl;     while (std::cin &gt;&gt; tempItem) {<br />        sumItem += tempItem;<br />        std::cout &lt;&lt; "If you want to enter another transaction, do so: " &lt;&lt; std::endl;<br />    }<br />    std::cout &lt;&lt; sumItem &lt;&lt; std::endl;<br />}<br />

Exercise 1.23

<br />int main() {<br />    Sales_item currItem, tempItem;<br />    int count = 1;<br />    std::cout &lt;&lt; "Enter the info for the first item" &lt;&lt; std::endl;     std::cin &gt;&gt; currItem;<br />    while (std::cin &gt;&gt; tempItem) {<br />        if (currItem.isbn() == tempItem.isbn()) {<br />            ++count;<br />        }<br />        else {<br />            std::cout &lt;&lt; currItem.isbn() &lt;&lt; " occurred " &lt;&lt; count &lt;&lt; " times " &lt;&lt; std::endl;<br />            currItem = tempItem;<br />            count = 1;<br />        }<br />    }<br />    std::cout &lt;&lt; currItem.isbn() &lt;&lt; " occurred " &lt;&lt; count &lt;&lt; " times " &lt;&lt; std::endl;<br />}<br />

Exercise 1.24

My code worked. I used the input 123,1,5,125,2,5,123,2,5,12,3,6,12,2,6 (where ‘,’ represents return) and got as output: ‘123 occurred 3 times, 12 occurred 2 times’ (where ‘,’ represents return).

Exercise 1.25

Write all that code into your editor, compile it and check it behaves as expected. It did for me.