Fundamentals of Programming - Southern Adventist University

1 downloads 170 Views 9MB Size Report
Feb 28, 2018 - If you are an instructor using this book in one or more of your courses, please let me know. Keeping trac
Fundamentals of

C++ Programming T F A DR

Richard L. Halterman School of Computing Southern Adventist University February 28, 2018

Copyright © 2008–2017 Richard L. Halterman. All rights reserved.

i

Contents

1

2

3

4

The Context of Software Development

1

1.1

Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

1.2

Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

1.3

Learning Programming with C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

1.4

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

Writing a C++ Program

7

2.1

General Structure of a Simple C++ Program . . . . . . . . . . . . . . . . . . . . . . . . .

7

2.2

Editing, Compiling, and Running the Program . . . . . . . . . . . . . . . . . . . . . . . .

8

2.3

Variations of our simple program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

2.4

Template for simple C++ programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

2.5

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

Values and Variables

15

3.1

Integer Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

3.2

Variables and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

3.3

Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

3.4

Additional Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

3.5

Floating-point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

25

3.6

Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27

3.7

Other Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

28

3.8

Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

28

3.9

Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31

3.10 Type Inference with auto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

3.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

Expressions and Arithmetic

37

©2017 Richard L. Halterman

Draft date: February 28, 2018

ii

CONTENTS

4.1

Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

37

4.2

Mixed Type Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

41

4.3

Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . .

44

4.4

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46

4.5

Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

47

4.6

Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

4.6.1

Compile-time Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

4.6.2

Run-time Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

51

4.6.3

Logic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

52

4.6.4

Compiler Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

53

4.7

Arithmetic Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

55

4.8

Integers vs. Floating-point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

58

4.8.1

Integer Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59

4.8.2

Floating-point Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . .

64

More Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

69

4.10 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

72

4.11 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

76

4.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

78

Conditional Execution

85

5.1

Type bool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

85

5.2

Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

86

5.3

The Simple if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

88

5.4

Compound Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

5.5

The if/else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

93

5.6

Compound Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

97

5.7

Nested Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

5.8

Multi-way if/else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

5.9

Errors in Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

4.9

5

5.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 6

Iteration

123

6.1

The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

6.2

Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

6.3

Abnormal Loop Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

©2017 Richard L. Halterman

Draft date: February 28, 2018

iii

CONTENTS

8

9

The break statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

6.3.2

The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

6.3.3

The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

6.4

Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

6.5

Iteration Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

6.6 7

6.3.1

6.5.1

Drawing a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

6.5.2

Printing Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

Other Conditional and Iterative Statements

159

7.1

The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

7.2

The Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

7.3

The do/while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

7.4

The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

7.5

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Using Functions

179

8.1

Introduction to Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

8.2

Standard Math Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

8.3

Maximum and Minimum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

8.4

clock Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

8.5

Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

8.6

Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

8.7

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

Writing Functions

201

9.1

Function Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

9.2

Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

9.3

Pass by Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

9.4

Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 9.4.1

Better Organized Prime Generator . . . . . . . . . . . . . . . . . . . . . . . . . . 217

9.4.2

Command Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

9.4.3

Restricted Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

9.4.4

Better Die Rolling Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

9.4.5

Tree Drawing Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

©2017 Richard L. Halterman

Draft date: February 28, 2018

iv

CONTENTS

9.4.6

Floating-point Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

9.4.7

Multiplication Table with Functions . . . . . . . . . . . . . . . . . . . . . . . . . 227

9.5

Commenting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

9.6

Custom Functions vs. Standard Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 231

9.7

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

10 Managing Functions and , y1="