C++ Primer #01 Getting Started

C++
C++ Primer Chapter 1

1. CHAPTER 1 GETTING STARTED

1.1 Writing a Simple C++ Program

Exercise 1.2:

Change the program to return -1. A return value of -1 is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator from main.


Return -1:

C++ code:

Console:

Return 0:

C++ code:

Console:

 

 

1.2 A First Look at Input/Output

Exercise 1.4:

Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead.


C++ code:

Shell:

 

 

Exercise 1.6

Explain whether the following program fragment is legal.

If the program is legal, what does it do? If the program is not legal, why not? How would you fix it?


Answer:

This program is not legal. Because there are 3 semicolons in between the source code which is unable for the compiler to compile. The fixed code is the following:

 

 

1.3 A Word about Comments

Exercise 1.8:

Indicate which, if any, of the following output statements are legal:


✔️

It prints out: /*


✔️

It prints out: */


✔️

It prints out: /*


✔️

It prints out: /*

 

1.4 Flow of Control

Exercise 1.9

Write a program that uses a while to sum the numbers from 50 to 100.


Code:

Console:

 

Exercise 1.10:

In addition to the ++ operator that adds 1 to its operand, there is a decrement operator (--) that subtracts 1. Use the decrement operator to write a while that prints the numbers from ten down to zero.


Code:

Console:

 

 

Exercise 1.11

Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.


Code:

Console:

 

Exercise 1.13

Rewrite the first two exercises from § 1.4.1 (p. 13) using for loops.


Rewrite 1.10:

Rewrite 1.11:

⚠️There is another solution which specify the start and end(aka. user defines the small and big)

You can even increment the value inside the loop:

 

Exercise 1.16

Write your own version of a program that prints the sum of a set of integers read from cin.


Oh my dear lord, I can't believe the method can works:

⚠️ Notice the for loop!!! Actually it makes sense once you take a breath and think. So the first argument sum is just an initial, std::cin >> value is always true until you exit, sum+=value is the incrementation. Cooooooool!!!

 

 

Example 1.4.4

Although this is not an exercise, it still deserves for a note. The program is expected to do the following:

If I input 10 10 10 28 28, it will prompt a message that 10 occurs 3 times right after I typed 28.

Console:

 

 

1.5 Introducing Classes

Exercise 1.20:

http://www.informit.com/title/0321714113 contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output.


Console:

 

Exercise 1.21:

Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.


Console:

⚠️ There is something missing from the above code: the else code block.

Hence we should add the following:

The full version should be:

 

 

1.6 The Bookstore Program

This is the code example of this section. However, I wrote something wrong which might be helpful for me to better understand and revise the code.

Console:

  1. The return value of 0 should not be inside the outmost if statement!! Instead, it should be after of the if statement like:

  1. I did not use the + operator to sum the object of Sales class.

However, it runs quite well. ✔️

The implementation should be:

Console:

 

 

That is the end of this chapter. Keep coding!

Previous
Previous

C++ Primer #02 Variables and Basic Types

Next
Next

Linagl#02 Elimination with Matrices