H.O.#2 Fall 2015 Gary Chan. Overview of C++ Programming

Similar documents
Programming. C++ Basics

Programming in C++ 4. The lexical basis of C++

Introduction to Programming

6.096 Introduction to C++ January (IAP) 2009

UEE1302 (1102) F10: Introduction to Computers and Programming

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C: How to Program. Week /Mar/05

Chapter 2: Overview of C. Problem Solving & Program Design in C

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

UNIT- 3 Introduction to C++

Chapter 2 - Introduction to C Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Creating a C++ Program

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

The C++ Language. Arizona State University 1

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Computer Programming : C++

Chapter 15 - C++ As A "Better C"

Fundamental of Programming (C)

Introduction to C++ Systems Programming

Variables. Data Types.

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Chapter 2. C++ Basics

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Basic Types, Variables, Literals, Constants

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

LECTURE 02 INTRODUCTION TO C++

C++ Programming: From Problem Analysis to Program Design, Third Edition

ANSI C Programming Simple Programs

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

COMP 202 Java in one week

Programming Fundamentals and Methodology. COMP104: C++ Basics

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CSCI 123 Introduction to Programming Concepts in C++

CS3157: Advanced Programming. Outline

Computer Components. Software{ User Programs. Operating System. Hardware

Fundamentals of Programming

Tokens, Expressions and Control Structures

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

ET156 Introduction to C Programming

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Objectives. In this chapter, you will:

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Full file at

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Introduction to Programming using C++

Chapter 1 & 2 Introduction to C Language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 2 - Control Structures

Programming with C++ as a Second Language

Introduction to C# Applications

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

This watermark does not appear in the registered version - Slide 1

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Programming and Data Structures

Introduction to Programming

Chapter 1 INTRODUCTION

Programming with C++ Language

Computer Components. Software{ User Programs. Operating System. Hardware

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

C Language, Token, Keywords, Constant, variable

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 2: Overview of C++

C++ Programming Basics

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

CSCI 1061U Programming Workshop 2. C++ Basics

Maciej Sobieraj. Lecture 1

IS 0020 Program Design and Software Tools

REVIEW. The C++ Programming Language. CS 151 Review #2

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Chapter 1 Introduction to Computers and C++ Programming

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Data types, variables, constants

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Lecture 2 Tao Wang 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

BASIC ELEMENTS OF A COMPUTER PROGRAM

BITG 1233: Introduction to C++

A Fast Review of C Essentials Part I

Chapter 2. Outline. Simple C++ Programs

A First Program - Greeting.cpp

COMP 202 Java in one week

Transcription:

H.O.#2 Fall 2015 Gary Chan Overview of C++ Programming

Topics C++ as a problem-solving tool Introduction to C++ General syntax Variable declarations and definitions Assignments Arithmetic operators COMP2012H (Overview) 2

What is C++? C++ is a programming language. A computer program performs a specific task, and may interact with the user and the computer hardware. Human work model: Computer work model: COMP2012H (Overview) 3

What is C++? One of the most popular programming languages: C++ (originally C) Basic Pascal Java Perl Cobol Scheme (Lisp) Smalltalk The common versions of C++: Microsoft Visual C++ g++ (for Unix machines) COMP2012H (Overview) 4

Why C++? Bad News: C++ is not so easy to learn Good News: Lots of good jobs for programmers Proficient in C++ makes you a good programmer (as it is a very disciplined language) C++ has wide applications in commercial, industrial and government sectors Though C++ is not the easiest language (Java and Python are easier), it is not the hardest either (Scheme, Prolog, and Assembly languages are really difficult!) COMP2012H (Overview) 5

C++ Software Development (More on This Later) Major steps 1. Editing (to write the program) 2. Compiling (creates.obj file) 3. Linking with compiled files (creates.exe file) Object files Library modules 4. Loading and executing 5. Testing the program Edit Source Program Compile Link Library routines Think Load Other object files COMP2012H (Overview) 6 Execute

Hardware Overview The variables and instructions are stored here Memory Input Devices Output Devices CPU COMP2012H (Overview) 7

Our First Program Preprocessor statements Function named main() indicates start of program // a simple program #include <iostream> Comments using namespace std; // space of variables int main() { cout << "Hello world!" << endl; return 0; } Ends execution of main() which ends program Print statement cssu5:~> g++ hello.cpp -o hello cssu5:~> hello Hello world! cssu5:~> end of line; \n or \n COMP2012H (Overview) 8

using namespace One of the problems encountered in C is that you run out of names for functions and identifiers It becomes harder to think of new names, e.g., using sort(), sort1(), sort_new(), etc., is not ideal In standard C++, each function and identifier lives in a region or space, to allow name reuse or prevent naming collision. This is done using using namespace keywords. Each set of C++ definitions in a library or program is wrapped in a namespace, and if some other definition has an identical name, but is in a different namespace, then there is no collision E.g., std::cout << hello\n ; // print to standard out The command using namespace is the context under which your program is run. You must be aware of namespace before you write any programs. Without it, if you simply include header file and use some functions or classes from that header, you may get some strange errors. All the standard C++ libraries are wrapped in a single namespace, which is std. COMP2012H (Overview) 9

Programming as Problem Solving Define the problem. What is the input & output? What constraints must be satisfied? What information is essential? Develop a solution Construct an algorithm (steps that must be done) Implement a program. Compile, test, and debug the program. Document and maintain the program. COMP2012H (Overview) 10

Example Problem Statement: Convert US dollars into Hong Kong dollars. Problem Analysis: Input: Amount in US$ Output: Amount in HK$ Apply official currency exchange rates. COMP2012H (Overview) 11

Example Algorithm Read in amount in US$ Calculate the HK$ amount Display the results COMP2012H (Overview) 12

Example // converts US$ to HK$ #include <iostream> using namespace std; int main(){ double usdollars; double hkdollars; // read in amount in US$ cout <<"Enter US$ amount and press return: "; cin >> usdollars; // calculate the HK$ amount hkdollars = 7.8 * usdollars; // display the results cout << "US$" << usdollars return 0; << " = HK$" << hkdollars << endl; } cssu5:~> g++ dollar.cpp o dollar cssu5:~> dollar Enter US$ amount and press return: 2.89 COMP2012H (Overview) 13 US$2.89 = HK$65.1464

Precision Printing (Printing to 2 decimal points) #include <iostream> using namespace std; int main(){ double x = 123.4567; cout.setf(ios::fixed); cout.precision(2); cout << x << endl; // ostream's functions // ostream's functions // or printf( %.2f\n, x ); //include <stdio.h> } return 0; cssu5:~> g++ dollar.cpp o dollar cssu5:~> a.out 123.45 COMP2012H (Overview) 14

Example Program Implementation: Program comments: // Library reference: #include Function type (for main): int Function name and (lack of) parameters: main( ) Statement braces: { } Variable definition: double Input/output functions: cin, cout COMP2012H (Overview) 15

What Makes a Good Program? Correctness Meets the problem requirements Produces correct results Easy to read and understand Easy to modify Easy to debug Efficient Fast Requires less memory COMP2012H (Overview) 16

Basic C++

General form of a C++ program C++ is a programming language for manipulating numbers and user-defined objects. //Program description is first #include directives go next using namespace std; int main(){ constant declarations/definitions go here variable declarations/definitions go here } assignment statements go here return 0; COMP2012H (Overview) 18

Declaration vs. Definition A variable provides us with named storage that we can write to, retrieve, and manipulate throughout the course of our program Determines the size and layout of its associated memory, the range of values that can be stored within that memory, and the set of operations that can be applied to it We speak of a variable, alternatively, as an object The definition of an object results in an allocation of storage int i; double j; char k; The declaration of an object makes known the type and name of the object without allocation of memory. An assertion that a definition of the variable exists elsewhere in the program extern int i; extern foo(); A program can contain only one definition of an object, but any number of object declarations The statement int i is (unfortunately) sometimes spoken as declaration statement, although defintion statement is more accurate COMP2012H (Overview) 19

General form of a C++ program //simple program #include <iostream> using namespace std; int main(){ // constant declaration statement const double Pi = 3.14159; // variable declaration statements double radius; double area; } // assignment statements cout << "Enter circle radius: "; cin >> radius; area = Pi * radius * radius; cout << "Area : " << area << endl; return 0; COMP2012H (Overview) 20

Syntax of the C++ Language Reserved words (appear in blue in Visual C++) Reserved words have a special meaning in C++. The list of reserved words: asm, auto, bool, break, case, catch, char, class, const, continue, default, delete, do, double, else, enum, extern, float, for, friend, goto, if, include, inline, int, long, namespace, new, operator, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, using, virtual, void, volatile, while COMP2012H (Overview) 21

Syntax of the C++ Language Identifiers (appear in black in Visual C++) An identifier is a name for variables, constants, functions, etc. It consists of a letter followed by any sequence of letters, digits or underscores Names are case-sensitive. The following are unique identifiers: Hello, hello, whoami, whoami, WhoAmI Names cannot have special characters in them e.g., X=Y, J-20, #007, etc. are invalid identifiers. C++ reserved words cannot be used as identifiers. Choose identifiers that are meaningful and easy to remember. COMP2012H (Overview) 22

Syntax of the C++ Language Comments (appear in green in Visual C++) Comments are explanatory notes; they are not part of the program. Comments are done in two ways: // A double slash starts a single line comment /* A slash followed by an asterisk marks the start of a multiple line comment. It ends with an asterisk followed by a slash */ COMP2012H (Overview) 23

Syntax of the C++ Language Compiler Directive: #include It refers to a header file of library functions or variables. The compiler reads in the contents of the file before compiling the program. The included file is compiled with the program. There are two forms of #include: #include <stdio.h> // for pre-defined files #include "my_lib.h" // for user-defined files COMP2012H (Overview) 24

Libraries #include loads the code from the standard libraries #include <iostream> // new I/O library #include <iostream.h> // old I/O library #include <stdio.h> // standard functions #include <math.h> // math functions #include <stdlib.h> // contains random funct #include <time.h> // time function Some constant declarations #define PI 3.14 #define e_charge 1.6e-19 #define EXCH_RATE 7.8 #define PLANK_h 6.626e-34 using namespace std; indicates that the new C++ libraries should be used. If this line is left out, then the old iostream library is loaded: #include <iostream.h> COMP2012H (Overview) 25

Constant Definition Constants represent permanent values. Their values can only be defined/set in the declaration statement: const double pi = 3.14159; They can make a program more readable and maintainable Constant definition syntax: const <type> <identifier> = <constant expression>; Examples: const double US2HK = 7.8; const double HK2Yuan = 0.8; const double US2Yuan = US2HK* HK2Yuan; COMP2012H (Overview) 26

Variable Definition A variable is best thought of as a container/box for a value: Variable definition syntax: <type> <identifier>; Examples: int nickel; int penny; 1000 A variable must be declared before it can be used. int main(){ x = 5; // illegal: x was not declared } COMP2012H (Overview) 27

Variable Definitions A variable can be initialized in a definition: int x = 3; Several variables of the same type can be defined in the same declaration statement (though it is sometimes better to put them on separate lines): double total_usd, area; A variable must have only one type. For example, a variable of the type int can only hold integer values. COMP2012H (Overview) 28

Types of Variable Definitions Simple C++ variable types (sizes are often machine-dependent): int integer (32-bit integer on PC) (example: 1) short 16-bit integer (allows ±32,767) long 32-bit integer (allows ±2,147,483,647) float 32-bit floating point number (allows about 7 digits of precision: 0.1234567) double 64-bit double precision float (allows about 15 digits of precision: 0.12345678901234) char 8-bit single character (example: y ) COMP2012H (Overview) 29

Memory Depiction float y = 12.5; int Temperature = 32; char Letter = 'c'; int Number; y Temperature Letter Number 12.5 32 'c' Memory location 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 COMP2012H (Overview) 30

Assignment Statements Assignment syntax: <identifier> = <expression>; Examples: int n, m, k; n = 5; m = 6 + (4 * 6); k = (n * 2) + m; k = k / 2; // definition COMP2012H (Overview) 31

Assignment Statements A variable must be assigned a value before it can be used. Variables are not automatically initialized in VC++. int x, y; // x declared, but not initialized // x and y have random values y = x; // the random value in x assigned to y Once a value has been placed in a variable, it stays there until the program changes it. COMP2012H (Overview) 32

Assignment Statements int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents ; NewStudents 6 NewStudents 6 OldStudents 21 OldStudents 21 TotalStudents - TotalStudents 27 COMP2012H (Overview) 33

Variable Assignments int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2; Value1 Value2 Hold 10 20 10 Value2 = Hold; Value1 Value2 Hold 20 20 10 Value1 20 Value2 10 Hold COMP2012H (Overview) 34 10

Arithmetic Operators Common Addition + Subtraction - Multiplication * Division / Mod % Note No exponentiation operator COMP2012H (Overview) 35

Integer Division Integer division produces an integer result It keeps the integral part Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 10 / 3 evaluates to 3-10 / 3 = -(10/3) evaluates to -3 COMP2012H (Overview) 36

Rules for Division C++ treats integers different than doubles. 100 is an int. 100.0, 100.0000, and 100. are doubles. The general rule for division of int and double types is: double/double -> double (normal) double/int -> double (normal) int/double -> double (normal) int/int -> int (special case: any decimal places discarded) COMP2012H (Overview) 37

Rules for Division Example : 220. / 100.0 double/double -> double result is 2.2 220. / 100 double/int -> double result is 2.2 220 / 100.0 int/double -> double result is 2.2 280 / 100 int/int -> int result is 2 Summary: division is normal unless both the numerator and denominator are int, then the result is an int (the decimal places are discarded). COMP2012H (Overview) 38

Forcing a Type Change You can change the type of an expression with a cast operation Syntax: variable1 = type(variable2); variable1 = type(expression); Example: int x=1, y=2; double result1 = x/y; // result1 is 0.0 double result2 = double(x)/y; // or (double) x / y; result2 is 0.5 double result3 = x/double(y); // result3 is 0.5 double result4 = double(x)/double(y); // result4 is 0.5 double result5 = double(x/y); // result5 is 0.0 COMP2012H (Overview) 39

Remainder a%b produces the remainder (NOT modulo) of the division i.e., (a/b) * b + a%b must equal a (hence a%b is the remainder given by a (a/b) * b) Examples 5 % 2 evaluates to 1 (=5-(5/2)*2) 12 % 4 evaluates to 0 (= 12 (12/4) * 4) 4 % 5 evaluates to 4 (= 4 (4/5) * 5) -4 % 5 evaluates to -4 (=-4 (-4/5) * 5) 4 % -5 evaluates to 4 (=4 (4/(-5))*(-5)) -4 % -5 evaluates to -4 (= -4 (-4/-5)*(-5)) Hence a%b is different from mod operator. (a mod b) always returns a non-negative integer, irrespective of the signs of a and b. COMP2012H (Overview) 40

Operators and Precedence Operator precedence tells how to evaluate expressions Standard precedence order ( ) Evaluated first, if nested innermost done first * / % Evaluated second. If there are several, then evaluate from left-to-right + - Evaluate third. If there are several, then evaluate from left-to-right Examples 1 + 2 * 3 / 4 5 = 1 + 6/4 5 = -3 2 * 4 / 5 + 3 * 5 % 4 = 8/5 + 15%4 = 1 + 3 = 4 3.0 * 3 / 4 = 9.0/4 = 2.25 (1 + 3) * ((2 + 4 * 6) * 3) / 2 + 2 = 4*( 26*3 )/2 + 2 = 158 COMP2012H (Overview) 41

Standard Input/Output cin - the standard input stream Input operator >> extracts data from input stream (the keyboard by default) skips over white spaces extracts only characters of the right form and performs automatic conversion to the type specified COMP2012H (Overview) 42

Standard Input/Output cout - the standard output stream Output operator << inserts data into the output stream (the screen by default) Example: int id, score; cout << "Enter student ID and score: "; cin >> id >> score; cout << "Student ID: " << id << " score: " << score << endl; COMP2012H (Overview) 43

// Convert inches to feet and inches // Input: inches // Output: feet and inches #include <iostream> using namespace std; #define PI 3.14 int main() { // inches to feet conversion factor const int in2feet = 12; int inches; int feet; // number of inches // number of feet cout<< "Enter number in inches: "; cin >> inches; } // Convert inches to feet and inches feet = inches / in2feet; inches = inches % in2feet; cout << feet << " feet " << inches << " inches " << endl; cout << Area of circle of radius 2 units is << PI * 4 << endl; return 0; COMP2012H (Overview) 44

Assignment Conversions A floating-point expression assigned to an integer object is rounded down An integer expression assigned to a floating-point object is converted to a floating-point value Example 1: float y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl; COMP2012H (Overview) 45

Assignment Conversions Example 2: int m, n; double x, y; m = 3; n = 2.5; // 2.5 converted to 2 and assigned to n x = m/n; // 3/2=1 converted to 1.0 and assigned to x n = x+m/2; // m/2=1 : integer division // x+m/2 : double addition because x is double // convert result of m/2 to double (i.e. 1.0) // x+m/2=2.0 // convert result of x+m/2 to int (i.e. 2) // because n is int COMP2012H (Overview) 46

Example: USD to HKD exchange Problem Statement Given a collection of nickels (US 5-cent piece) and pennies (US 1-cent piece), find the equivalent number of Hong Kong dollars and 10-cent pieces. Problem Analysis Input: nickels (integer): number of US nickels (5 cents) pennies (integer): number of US pennies (1 cent) A US dime is 10 cents Output: dollars (integer): number HK dollar coins to return houji (integer): number HK 10-cent coins to return Constraints: None COMP2012H (Overview) 47

Example: Initial Algorithm 1. Read in the numbers of nickels and pennies 2. Compute the total value in integral US dollars 3. Exchange the integral USD to HK dollars based on exchange rate 4. Find the number of HK dollar coins and houji coins 5. Display the results COMP2012H (Overview) 48

Example: Program Skeleton // File: excoin.cpp // Determines the number of HK coins to exchange for US coins #include <iostream> using namespace std; int main(){ int nickel; // number of nickels int penny; // number of pennies int dollar; // number of HK dollar coins int houji; // number of HK 10-cent coins double total_usd; // total value in US$ double total_hkd; // total value in HK$ // Read in the number of nickels and pennies // Compute the total value in US$ // Compute the total value in HK$ to exchange // Find the numbers of HK dollar and 10-cent coins // Display the numbers of HK dollar and 10-cent coins return 0; } COMP2012H (Overview) 49

Example: Refined Algorithm 1. Read in the number of nickels and pennies 2. Compute the total value in US dollars total_usd = (5 * nickel + penny)/100 3. Compute the total in HK dollars to exchange total_hkd = total_usd * US2HK 4. Find the number of HK dollar coins and 10-cent coins total_hk_cent = total_hkd * 100 dollar = total_hk_cent / 100 houji = (total_hk_cent % 100) / 10 5. Display the number of HK dollar and 10-cent coins COMP2012H (Overview) 50

C++ Arithmetic Operators The four operators +, -, *, and / work as we expect with the normal precedence rules (e.g., 5+2*3 = 11) Parenthesis can be inserted to change the order of operations (e.g., (5+2)*3 = 21) Be careful of integer division -- any remainder is discarded The % (modulo) operator gives the remainder of integer division COMP2012H (Overview) 51

// File: excoin.cpp // Determines the number of HK coins to exchange for US coins #include <iostream> using namespace std; int main(){ const double US2HK = 7.8; // assume exchange rate is US$1 = HK$7.8 int nickel; // number of nickels int penny; // number of pennies int dollar; // number of HK dollar coins int houji; // number of HK 10-cent coins double total_usd; // total value in US$ int total_hk_cent; // total value in HK cents double total_hkd; // total value in HK$ // Read in the number of nickels and pennies cout << "Enter the number of nickels and press return: "; cin >> nickel; cout << "Enter the number of pennies and press return: "; cin >> penny; COMP2012H (Overview) 52

// Compute the total value in US$ total_usd = (5 * nickel + penny) / 100.0; // Compute the total value in HK$ using the assumed exchange rate total_hkd = total_usd * US2HK; // Find the value in HK dollars and change total_hk_cent = total_hkd * 100; dollar = total_hk_cent / 100; houji = (total_hk_cent % 100)/10; A faster way: dollar = total_hkd; // implicit cast houji = 10 * (total_hkd dollar); // Display the number of HK dollar and 10-cent coins cout << "The change is HK " << dollar << " dollars and " << houji << " 10-cent coins." << endl; return 0; } COMP2012H (Overview) 53

C++ is a Free-Format Language Extra blanks or tabs are ignored x=3; x = 3 ; Blank lines are ignored just like comments Code can be indented in any way More than one statement can be on one line int x, y; x=3; y = 10; int z = x+y; A single statement can be continued over several lines int x = 2; cout << feet << " feet and " << inches << " inches" << endl; COMP2012H (Overview) 54

Good Programming Style Place each statement on a line by itself (except long cout statements) x = m/n; Use blank lines to separate sections of code Use the same indentation for all statements in the same block of code { }. int main(){ int x; x = 5; } return 0; COMP2012H (Overview) 55

Good Programming Style Use meaningful identifier names double area, sum, radius; Document each variable when it is declared double area; double distance; Document each segment of code // Convert inches to feet and inches feet = inches / in2feet; inches = inches % in2feet; // area of the circle // distance from top to bottom Document the beginning of the program with a header that tells the purpose of the program // Convert inches to feet and inches COMP2012H (Overview) 56