Вы находитесь на странице: 1из 11

Introduction to C++ Programming I

Ian Aitchison and Peter King August 1997

Contents
1 The Computer 11 1.1 Central Processing Unit . . . . . . . . . . . . . . . . . . . . . 13 1.2 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.1 Main memory . . . . . . . . . . . . . . . . . . . . . . . 14 1.2.2 External Memory . . . . . . . . . . . . . . . . . . . . . 14 1.3 Input/Output Devices . . . . . . . . . . . . . . . . . . . . . . 15 1.4 The system bus . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.5 More about memory and information representation . . . . . 16 1.5.1 Representation of information in external memory . . 17 1.6 The execution cycle . . . . . . . . . . . . . . . . . . . . . . . 17 1.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.8 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 19 1.9 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 Programming Languages 20 2.1 Assembly Language . . . . . . . . . . . . . . . . . . . . . . . 21 2.2 High level Languages . . . . . . . . . . . . . . . . . . . . . . . 22 2.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.4 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 23 3 Operating Systems 25 3.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.2 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 27 4 Preparing a Computer Program 29 4.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.2 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 31 4.3 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 31 5 Algorithms 32 5.1 Describing an Algorithm . . . . . . . . . . . . . . . . . . . . . 33 5.2 Statements required to describe algorithms . . . . . . . . . . 35 5.3 Verifying the correctness of the algorithm . . . . . . . . . . . 37 5.3.1 Desk-checking . . . . . . . . . . . . . . . . . . . . . . . 38 1

5.4 Series Minimum and Maximum Algorithm . . . . . . . . . . . 38 5.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.6 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 42 5.7 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 42 5.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 6 A simple C++ program 45 6.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 6.1.1 Reserved words . . . . . . . . . . . . . . . . . . . . . . 48 6.2 Declaration of variables . . . . . . . . . . . . . . . . . . . . . 49 6.3 Constants and the declaration of constants . . . . . . . . . . 51 6.4 General form of a C++ Program . . . . . . . . . . . . . . . . 52 6.5 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . 52 6.6 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . 55 6.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 6.8 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 56 6.9 Review questions . . . . . . . . . . . . . . . . . . . . . . . . . 57 6.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 7 The Assignment statement 59 7.1 Priority of Operators . . . . . . . . . . . . . . . . . . . . . . . 61 7.2 Examples of Arithmetic Expressions . . . . . . . . . . . . . . 62 7.3 Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . 62 7.4 Example Program: Temperature Conversion . . . . . . . . . . 62 7.5 Example Program: Pence to Pounds and Pence . . . . . . . . 63 7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 7.7 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 64 7.8 Review questions . . . . . . . . . . . . . . . . . . . . . . . . . 65 7.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 8 Further Assignment Statements & Control of Output 67 8.1 Increment and Decrement Operators . . . . . . . . . . . . . . 67 8.2 Specialised Assignment Statements . . . . . . . . . . . . . . . 68 8.3 Formatting of output . . . . . . . . . . . . . . . . . . . . . . . 69 8.4 Example Program: Tabulation of sin function . . . . . . . . . 71 8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 8.6 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 73 8.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 9 Introduction to structured design 75 9.1 Conditional Control Structures . . . . . . . . . . . . . . . . . 75 9.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 9.3 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 77 9.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 2

10 Conditions 79 10.1 Relational Expressions . . . . . . . . . . . . . . . . . . . . . . 79 10.2 Examples using Relational Operators . . . . . . . . . . . . . . 79 10.3 Logical Expressions . . . . . . . . . . . . . . . . . . . . . . . . 80 10.4 Examples using logical operators . . . . . . . . . . . . . . . . 81 10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 10.6 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 82 10.7 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 83 11 The if statement 85 11.1 Examples of if statements . . . . . . . . . . . . . . . . . . . 85 11.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 11.3 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 87 11.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 12 The if-else Statement 89 12.1 Examples of if-else statements . . . . . . . . . . . . . . . . 90 12.2 Example Program: Wages Calculation . . . . . . . . . . . . . 90 12.3 Example Program: Pythagorean Triples . . . . . . . . . . . . 91 12.4 Example Program: Area and Perimeter of Rectangle . . . . . 93 12.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 12.6 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 94 12.7 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 95 12.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 13 Nested if and if-else statements 97 13.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 13.2 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 99 13.3 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 99 13.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 14 The switch statement 101 14.1 Examples of switch statements . . . . . . . . . . . . . . . . . 102 14.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 14.3 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 104 14.4 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 104 14.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 15 Further Structured Design 106 15.1 Repetition Control Structures . . . . . . . . . . . . . . . . . . 106 15.2 Example One: Using a while loop . . . . . . . . . . . . . . . 106 15.3 Example Two: Using a while loop . . . . . . . . . . . . . . . 109 15.4 Other forms of Repetition Control Structures . . . . . . . . . 110 15.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 3

15.6 Review questions . . . . . . . . . . . . . . . . . . . . . . . . . 112 15.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 16 The while statement 114 16.1 Example while loop: Printing integers . . . . . . . . . . . . . 115 16.2 Example while loop: Summing Arithmetic Progression . . . 115 16.3 Example while loop: Table of sine function . . . . . . . . . . 116 16.4 Example while loop: Average, Minimum and Maximum Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 16.5 Example Program: Student mark processing . . . . . . . . . . 119 16.6 Example Program: Iterative evaluation of a square root . . . 122 16.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 16.8 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 125 16.9 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 126 16.10Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 17 The do-while statement 129 17.1 Example Program: Sum of Arithmetic Progression . . . . . . 130 17.2 Example Program: Valid Input Checking . . . . . . . . . . . 130 17.3 Example Program: Student Mark Processing (2) . . . . . . . 131 17.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 17.5 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 132 17.6 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 133 17.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 18 The for statement 135 18.1 Example for statement: Print 10 integers . . . . . . . . . . . 136 18.2 Example for statement: Print table of sine function . . . . . 136 18.3 Example Program: Student Mark Processing (3) . . . . . . . 137 18.4 Example Program: Generation of Pythagorean Triples . . . . 138 18.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 18.6 Multiple Choice Questions . . . . . . . . . . . . . . . . . . . . 140 18.7 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 141 18.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 19 Streams and External Files 144 19.1 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 19.2 Connecting Streams to External Files . . . . . . . . . . . . . 145 19.3 Testing for end-of-file . . . . . . . . . . . . . . . . . . . . . . . 146 19.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 19.5 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 149 19.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 4

20 Top-down design using Functions 150 20.1 The need for functions . . . . . . . . . . . . . . . . . . . . . . 151 20.2 The mathematical function library in C++ . . . . . . . . . . 152 20.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 20.4 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 154 20.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 21 Introduction to User-defined functions in C++ 155 21.1 Functions with no parameters . . . . . . . . . . . . . . . . . . 156 21.2 Functions with parameters and no return value . . . . . . . . 158 21.3 Functions that return values . . . . . . . . . . . . . . . . . . . 160 21.4 Example function: sum of squares of integers . . . . . . . . . 162 21.5 Example Function: Raising to the power . . . . . . . . . . . . 162 21.6 Call-by-value parameters . . . . . . . . . . . . . . . . . . . . . 163 21.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 21.8 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 164 21.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 22 Further User-defined functions in C++ 167 22.1 Call-by-reference parameters . . . . . . . . . . . . . . . . . . 167 22.2 Example Program: Invoice Processing . . . . . . . . . . . . . 169 22.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 22.4 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 174 22.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 23 Arrays 176 23.1 Arrays in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . 177 23.1.1 Declaration of Arrays . . . . . . . . . . . . . . . . . . 177 23.1.2 Accessing Array Elements . . . . . . . . . . . . . . . . 178 23.1.3 Initialisation of arrays . . . . . . . . . . . . . . . . . . 180 23.2 Example Program: Printing Outliers in Data . . . . . . . . . 180 23.3 Example Program: Test of Random Numbers . . . . . . . . . 182 23.4 Arrays as parameters of functions . . . . . . . . . . . . . . . . 184 23.5 Strings in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . 186 23.5.1 String Output . . . . . . . . . . . . . . . . . . . . . . 187 23.5.2 String Input . . . . . . . . . . . . . . . . . . . . . . . . 187 23.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 23.7 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 190 23.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 5

About the course


Welcome to Heriot-Watt University Introduction to Computing I course. This course is being provided for students taking the Certificate in Science of Aberdeen and Heriot-Watt Universities. It is being delivered by way of the World Wide Web, however all students will also receive the entire course in a printed format. The course is arranged in a series of Lessons. As well as new material to learn, each Lesson contains review questions, multiple choice questions, and programming exercises. The review questions are designed to get you to think about the contents of the Lesson. These review questions should be straightforward; if the answer is not obvious, reread the Lesson. The multi- ple choice questions can be answered on-line when you are using the World Wide Web; feedback is given if you choose the wrong answers. The program- ming exercises are an essential part of the course. Computer programming is a practical subject, without practice no progress will be made. There will be some assessed coursework, this may be submitted by elec- tronic mail. Electronic mail will also be used to provide help with problems.

Course contents
This course is intended as a first introduction to programming computers using the C++ programming language. It is not assumed that the student has done any programming before hence this course is not comprehensive and does not cover all of C++. In particular it does not cover any of the object-oriented features of C++, these are introduced in the following course (Introduction to Computing II). Because this is a first programming course emphasis is placed on the design of programs in a language-independent fashion. A brief introduction to computers is also given. The lessons of the course may be split into groups as follows: 1. About the computer and computer systems. Lesson 1 - The Computer. Covers the Central processor, mem- ory, information representation and the operation cycle of the computer. 6

Lesson 2 - Programming Languages. Covers the various levels of Programming Languages. Lesson 3 - Operating Systems. Covers the purpose of Operating systems and the major types. Lesson 4 - Preparing a Computer Program. Covers the steps that are carried out in going from a problem specification to having a well-tested and reliable program to solve the problem. 2. About the design of programs. Lesson 5 - Algorithms. The basic constructs used in designing programs. Sequence, selection and repetition. Lesson 9 - Introduction to Structured Design. Top-down design of algorithms using sequence and selection only. Lesson 15 - Further Structured Design. Top-down design of algo- rithms using repetition. Lesson 20 - Top-down design using Functions. An introduction to problem-solving by splitting the problem into sub-problems whose solutions are implemented as functions. 3. About C++ Lesson 6 - A simple C++ program. Looks at a simple C++ pro- gram and identifies features. Covers simple ideas about variables and their declaration and input and output. Lesson 7 - The Assignment statement. How values are assigned to variables. Lesson 8 - Further Assignment Statements & Control of Output. More forms of assignment statement and simple formatting of output. Lesson 10 - Conditions. How expressions that can be true or false are written in C++. Lesson 11 - The if statement. How conditional execution of a statement is carried out. Lesson 12 - The if-else statement. How a choice can be made between which of two statements to execute. Lesson 13 - Nested if and if-else statements. How multi-choice decisions can be made. Lesson 14 - The switch statement. An alternative way to imple- ment multi-choice conditions. Lesson 16 - The while statement. The basic way to carry out repetition. 7

Lesson 17 - The do-while statement. An alternative way to carry out repetition. Lesson 18 - The for statement. Repetition a set number of times or as a control loop variable takes a set of values. Lesson 19 - Streams and External Files. How to input and output data from and to external files. Lesson 21 - An introduction to User-defined functions in C++. How to design your own functions. Lesson 22 - Further User-defined functions. Returning informa- tion by parameters. Lesson 23 - Arrays. How to work with large collections of indexed data. Note that there is also a document The Computer Exercises which gives some help in using Windows and the Borland C++ compilers.

Suggested study timetable


It is suggested that you should cover the contents of the course in about 13/14 weeks. The lessons are not equal in size or in the difficulty of their content. The following timetable is suggested. Week 1 Try to cover most of the material in Lessons 1 to 4 during this first week. This is mainly background material for those who have not had much contact with computers before but some (or all) of it will probably be familiar to most people taking the course. During this first week try to familiarise yourself with the computer system you are going to use so that you are ready to start programming as soon as possible. Included with the course notes (The Computer Exercises) is a document about using the Borland C++ compilers, check over this and go through the suggested steps in compiling a small supplied C++ program. This activity may well expand into week 2. Week 2 Read Lesson 5 on Algorithms. Dont worry if you find it difficult to make up your own algorithms at this stage, but do make an at- tempt. Youll continue to get practice at this as long as you program! Also start looking at Lesson 6 on simple programs and their features. Implement your solutions to the exercises on the computer. Week 3 Cover Lessons 7 and 8 on assignment and implement the exercises on simple calculation type programs. Week 4 Now study Lesson 9 on Algorithms with simple selection. Read Lesson 10 on how conditional expressions are constructed in C++. 8

Week 5 Study Lessons 11 and 12 on if and if-else statements and implement the exercise on programs with simple selection. Week 6 Continue the study of selection mechanisms with Lessons 13 and 14 on nested if and if-else statements and the switch statement. The switch statement is not as fundamental as the others so spend less time on it than the other selection mechanisms. Week 7 Study Lesson 15 on repetition constructs and practice designing algorithms using repetition. Commence Lesson 16 on the while state- ment. Week 8 Continue Lesson 16 on the while statement. Also cover Lesson 17 on the do-while statement this week. Do not spend so much time on it as on the more fundamental while statement. Week 9 Cover Lesson 18 on the for statement. This statement is used frequently and so is an important statement. Have a look at Lesson 19 on Streams and Files so that you can use an External file in the last assignment. Week 10 Study Lesson 20 on structured design using functions. This is a very important Lesson. If you have time start on Lesson 21 on how to construct C++ functions. Week 11 Carry on with Lesson 21 on user-defined functions. Continue on to Lesson 22. In carrying out the exercises in these Lessons you will also get further practice on using the conditional and repetition control structures. Week 12 Study Lesson 23 which introduces the concept of arrays. Again this is an important concept in programming. The exercises for this chapter will provide you with more practice in the use of control struc- tures and functions. Week 13 Finish off the course and start revision.

Assessment
There will be two class assignments to carry out. One will be given out which requires that you know the material up to and including Lesson 12. The other will require that you know the material up to and including Lesson 20 (19 is not needed for the assignment). The first assignment will be worth 10% in the final assessment and the second assignment will be worth 15% in the final assessment. The remaining 75% will come from the class examination. Further information will be available on the World Wide Web version of the course. 9