0% found this document useful (0 votes)
38 views

0445 Fundamentals of C Programming

Uploaded by

PaulArsenie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

0445 Fundamentals of C Programming

Uploaded by

PaulArsenie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Fundamentals of

C++
Programming
A F T
DR
Richard L. Halterman
School of Computing
Southern Adventist University

December 2, 2018
Copyright © 2008–2018 Richard L. Halterman. All rights reserved.
i

Contents

1 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

2 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

3 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

4 Expressions and Arithmetic 37

©2018 Richard L. Halterman Draft date: December 2, 2018


CONTENTS ii

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
4.9 More Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.10 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.11 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

5 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
5.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

6 Iteration 123
6.1 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
6.2 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6.3 Abnormal Loop Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

©2018 Richard L. Halterman Draft date: December 2, 2018


CONTENTS iii

6.3.1 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.5.1 Drawing a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
6.5.2 Printing Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
6.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

7 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

8 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

9 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

©2018 Richard L. Halterman Draft date: December 2, 2018


Click here to download full PDF material

You might also like