Chapter 2 Primitive Data Types and Operations Liang

  • Slides: 60
Download presentation
Chapter 2 Primitive Data Types and Operations Liang, Introduction to C++ Programming, (c) 2007

Chapter 2 Primitive Data Types and Operations Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 1

Objectives F F F F To write C++ programs to perform simple calculations (§

Objectives F F F F To write C++ programs to perform simple calculations (§ 2. 2). To read input from the keyboard using the cin object (§ 2. 3). To simplify programming by omitting the std: : prefix (§ 2. 4). To use identifiers to name variables, constants, functions, and classes (§ 2. 5). To use variables to store data (§§ 2. 6 -2. 7). To program with assignment statements and assignment expressions (§ 2. 7). To use constants to store permanent data (§ 2. 8). To declare variables using numeric data types (§ 2. 9). To use operators to write numeric expressions (§ 2. 9). To convert numbers to a different type using casting (§ 2. 10). To represent character using the char type (§ 2. 11). To become familiar with C++ documentation, programming style, and naming conventions (§ 2. 13). To distinguish syntax errors, runtime errors, and logic errors (§ 2. 14). To debug logic errors (§ 2. 15). Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 2

Introducing Programming with an Example Listing 2. 1 Computing the Area of a Circle

Introducing Programming with an Example Listing 2. 1 Computing the Area of a Circle This program computes the area of the circle. Compute. Area Run Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 3

animation Trace a Program Execution allocate memory for radius #include <iostream> int main() {

animation Trace a Program Execution allocate memory for radius #include <iostream> int main() { double radius; double area; radius no value // Step 1: Read in radius = 20; // Step 2: Compute area = radius * 3. 14159; // Step 3: Display the area std: : cout << "The area is "; std: : cout << area << std: : endl; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 4

animation Trace a Program Execution #include <iostream> int main() { double radius; double area;

animation Trace a Program Execution #include <iostream> int main() { double radius; double area; // Step 1: Read in radius = 20; memory radius no value area no value allocate memory for area // Step 2: Compute area = radius * 3. 14159; // Step 3: Display the area std: : cout << "The area is "; std: : cout << area << std: : endl; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 5

animation Trace a Program Execution assign 20 to radius #include <iostream> int main() {

animation Trace a Program Execution assign 20 to radius #include <iostream> int main() { double radius; double area; radius area 20 no value // Step 1: Read in radius = 20; // Step 2: Compute area = radius * 3. 14159; // Step 3: Display the area std: : cout << "The area is "; std: : cout << area << std: : endl; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 6

animation Trace a Program Execution #include <iostream> int main() { double radius; double area;

animation Trace a Program Execution #include <iostream> int main() { double radius; double area; // Step 1: Read in radius = 20; // Step 2: Compute area = radius * 3. 14159; memory radius area 20 1256. 636 compute area and assign it to variable area // Step 3: Display the area std: : cout << "The area is "; std: : cout << area << std: : endl; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 7

animation Trace a Program Execution #include <iostream> int main() { double radius; double area;

animation Trace a Program Execution #include <iostream> int main() { double radius; double area; memory radius area // Step 1: Read in radius = 20; // Step 2: Compute area = radius * 3. 14159; 20 1256. 636 print a message to the console // Step 3: Display the area std: : cout << "The area is "; std: : cout << area << std: : endl; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 8

Reading Input from the Keyboard You can use the std: : cin object to

Reading Input from the Keyboard You can use the std: : cin object to read input from the keyboard. Compute. Area 1 Run Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 9

Omitting the std: : Prefix You have noticed that std: : cout, std: :

Omitting the std: : Prefix You have noticed that std: : cout, std: : endl, and std: : cin all start with std: : . So what is std? std means the standard namespace. C++ divides the world into “namespaces” to resolve potential naming conflicts. std: : cout means that cout belongs to the standard namespace. It is tedious to type std: : repeatedly. There are two solutions to eliminate the std: : prefix. The first solution is to add the statement: using namespace std; Compute. Area 2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X Run 10

cin/cout F std: : cout << "Welcome to C++!" ; – std: : cout

cin/cout F std: : cout << "Welcome to C++!" ; – std: : cout : stands for console output; – << : stream insertion operator, sends string to the console. F std: : cin >> radius; – std: : cin : stands for console input – >> : stream extraction operator, assigns an input to a variable Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 11

Identifiers F An identifier is a sequence of characters that consists of letters, digits,

Identifiers F An identifier is a sequence of characters that consists of letters, digits, and underscores (_). F An identifier must start with a letter or an underscore. It cannot start with a digit. F An identifier cannot be a reserved word. (See Appendix A, “C++ Keywords, ” for a list of reserved words. ) F An identifier can be of any length, but your C++ compiler may impose some restriction. Use identifiers of 31 characters or fewer to ensure portability. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 12

Variables // Compute the first area radius = 1. 0; area = radius *

Variables // Compute the first area radius = 1. 0; area = radius * 3. 14159; std: : cout << area; // Compute the second area radius = 2. 0; area = radius * 3. 14159; std: : cout << area; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 13

Declaring Variables Syntax for declaring variables Data. Type Variable. Name; int x; // Declare

Declaring Variables Syntax for declaring variables Data. Type Variable. Name; int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; int i, j, k; // // Declare a to be a character variable; declare I, j, k to be integers Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 14

Assignment Statements Syntax for the assignments statement Variable = expression; x = 1; //

Assignment Statements Syntax for the assignments statement Variable = expression; x = 1; // Assign 1 to x; radius = 1. 0; // Assign 1. 0 to radius; a = 'A'; // Assign 'A' to a; x = x + 1; cout<<(x + 1); Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 15

Declaring and Initializing in One Step F int x = 1; – int x;

Declaring and Initializing in One Step F int x = 1; – int x; – x = 1; F double F int d = 1. 4; i(1), j(2); – int I = 1, j = 2; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 16

Named Constants const datatype CONSTANTNAME = VALUE; const double PI = 3. 14159; const

Named Constants const datatype CONSTANTNAME = VALUE; const double PI = 3. 14159; const int SIZE = 3; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 17

Numerical Data Types Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All

Numerical Data Types Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 18

sizeof Function You can use the sizeof function to find the size of a

sizeof Function You can use the sizeof function to find the size of a type. For example, the following statement displays the size of int, long, and double on your machine. cout << sizeof(int) << " " << sizeof(long) << " " << sizeof(double); Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 19

Synonymous Types short int is synonymous to short. unsigned short int is synonymous to

Synonymous Types short int is synonymous to short. unsigned short int is synonymous to unsigned short. unsigned int is synonymous to unsigned. long int is synonymous to long. unsigned long int is synonymous to unsigned long. For example, short int i = 2; is same as short i = 2; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 20

Numeric Literals A literal is a constant value that appears directly in a program.

Numeric Literals A literal is a constant value that appears directly in a program. For example, 34, 1000000, and 5. 0 are literals in the following statements: int i = 34; long k = 1000000; double d = 5. 0; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 21

octal and hex literals By default, an integer literal is a decimal number. To

octal and hex literals By default, an integer literal is a decimal number. To denote an octal integer literal, use a leading 0 (zero), and to denote a hexadecimal integer literal, use a leading 0 x or 0 X (zero x). For example, the following code displays the decimal value 65535 for hexadecimal number FFFF and decimal value 8 for octal number 10. cout << 0 x. FFFF << " " << 010; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 22

why called floating-point? The float and double types are used to represent numbers with

why called floating-point? The float and double types are used to represent numbers with a decimal point. Why are they called floating-point numbers? These numbers are stored into scientific notation. When a number such as 50. 534 e+1 is converted into scientific notation such as 5. 0534, its decimal point is moved (i. e. , floated) to a new position. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 23

Numeric Operators Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights

Numeric Operators Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 24

Integer Division +, -, *, /, and % 5 / 2 yields an integer

Integer Division +, -, *, /, and % 5 / 2 yields an integer 2. 5. 0 / 2 yields a double value 2. 5 5 % 2 yields 1 (the remainder of the division) Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 25

Remainder Operator Remainder is very useful in programming. For example, an even number %

Remainder Operator Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. Suppose today is Saturday and your friends are going to meet in 10 days. What day is in 10 days? You can find that day is Tuesday using the following expression: Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 26

Example: Displaying Time Write a program that obtains hours and minutes from seconds. Display.

Example: Displaying Time Write a program that obtains hours and minutes from seconds. Display. Time Run Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 27

Overflow When a variable is assigned a value that is too large to be

Overflow When a variable is assigned a value that is too large to be stored, it causes overflow. For example, executing the following statement causes overflow, because the largest value that can be stored in a variable of the short type is 32767. 32768 is too large. short value = 32767 + 1; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 28

Underflow When a variable is assigned a value that is too small to be

Underflow When a variable is assigned a value that is too small to be stored, it causes underflow. For example, executing the following statement causes underflow, because the smallest value that can be stored in a variable of the short type is -32768. 32769 is too small. short value = -32769; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 29

Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y) Liang, Introduction

Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y) Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 30

Example: Converting Temperatures Write a program that converts a Fahrenheit degree to Celsius using

Example: Converting Temperatures Write a program that converts a Fahrenheit degree to Celsius using the formula: Fahrenheit. To. Celsius Run Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 31

Shorthand Assignment Operators Operator Example Equivalent += i += 8 i = i +

Shorthand Assignment Operators Operator Example Equivalent += i += 8 i = i + 8 -= f -= 8. 0 f = f - 8. 0 *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 %= i %= 8 i = i % 8 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 32

Increment and Decrement Operators Operator ++var Name preincrement var++ postincrement --var predecrement var-- postdecrement

Increment and Decrement Operators Operator ++var Name preincrement var++ postincrement --var predecrement var-- postdecrement Description The expression (++var) increments var by 1 and evaluates to the new value in var after the increment. The expression (var++) evaluates to the original value in var and increments var by 1. The expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. The expression (var--) evaluates to the original value in var and decrements var by 1. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 33

Increment and Decrement Operators, cont. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education,

Increment and Decrement Operators, cont. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 34

Increment and Decrement Operators, cont. Using increment and decrement operators makes expressions short, but

Increment and Decrement Operators, cont. Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i. ++ and -- can be applied to all integers and floating-point types Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 35

Numeric Type Conversion Consider the following statements: short i = 100; long k =

Numeric Type Conversion Consider the following statements: short i = 100; long k = i * 3 + 4; double d = i * 3. 1 + k / 2; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 36

Conversion Rules When performing a binary operation involving two operands of different types, C++

Conversion Rules When performing a binary operation involving two operands of different types, C++ automatically converts the operand based on the following rules: 1. 2. 3. 4. 5. 6. 7. If one of the operands is long double, the other is converted into long double. Otherwise, if one of the operands is double, the other is converted into double. Otherwise, if one of the operands is float, the other is converted into float. Otherwise, if one of the operands is unsigned long, the other is converted into unsigned long. Otherwise, if one of the operands is long, the other is converted into long. Otherwise, if one of the operands is unsigned int, the other is converted into unsigned int. Otherwise, both operands are converted into int. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 37

Type Casting Implicit casting double d = 3; (type widening) Explicit casting int i

Type Casting Implicit casting double d = 3; (type widening) Explicit casting int i = static_cast<int>(3. 0); (type narrowing) int i = (int)3. 9; (Fraction part is truncated) Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 38

NOTE Casting does not change the variable being cast. For example, d is not

NOTE Casting does not change the variable being cast. For example, d is not changed after casting in the following code: double d = 4. 5; int i = static_cast<int>(d); // d is not changed Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 39

NOTE The GNU C++ compiler will give a warning when you narrow a type

NOTE The GNU C++ compiler will give a warning when you narrow a type unless you use static_cast to make the conversion explicit. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 40

Example: Keeping Two Digits After Decimal Points Write a program that displays the sales

Example: Keeping Two Digits After Decimal Points Write a program that displays the sales tax with two digits after the decimal point. Sales. Tax Run Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 41

Character Data Type char letter = 'A'; (ASCII) char num. Char = '4'; (ASCII)

Character Data Type char letter = 'A'; (ASCII) char num. Char = '4'; (ASCII) NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding character. For example, the following statements display character b. char ch = 'a'; cout << ++ch; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 42

Read Characters To read a character from the keyboard, use cout << "Enter a

Read Characters To read a character from the keyboard, use cout << "Enter a character: "; char ch; cin >> ch; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 43

Escape Sequences for Special Characters Liang, Introduction to C++ Programming, (c) 2007 Pearson Education,

Escape Sequences for Special Characters Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 44

Appendix B: ASCII Character Set is a subset of the Unicode from u 0000

Appendix B: ASCII Character Set is a subset of the Unicode from u 0000 to u 007 f Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 45

ASCII Character Set, cont. ASCII Character Set is a subset of the Unicode from

ASCII Character Set, cont. ASCII Character Set is a subset of the Unicode from u 0000 to u 007 f Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 46

Casting between char and Numeric Types Ø when an integer is cast into a

Casting between char and Numeric Types Ø when an integer is cast into a char, only its lower 8 bits of data are used. Øchar c = 0 x. FF 41; Øcout<<c; //c is character A ØWhen a floating-point value is cast into a char, the floating-point value is first cast into an int, which is then cast into char. Øchar c = 65. 25; Øcout<<c; // c is character A. Ø when a char is cast into numeric type, the character’s ASCII is cast into the specified numeric type. int i = 'a'; // Same as int i = (int)'a'; char c = 97; // Same as char c = (char)97; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 47

Numeric Operators on Characters The char type is treated as if it is an

Numeric Operators on Characters The char type is treated as if it is an integer of the byte size. All numeric operators can be applied to char operands. A char operand is automatically cast into a number if the other operand is a number or a character. For example, the following statements int i = '2' + '3'; // (int)'2' is 50 and (int)'3' is 51 cout << "i is " << i << endl; // i is decimal 101 int j = 2 + 'a'; // (int)'a' is 97 cout << "j is " << j << endl; cout << j << " is the ASCII code for character " << static_cast<char>(j) << endl; Display i is 101 j is 99 99 is the ASCII code for character c Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 48

Note It is worthwhile to note that the ASCII for lowercase letters are consecutive

Note It is worthwhile to note that the ASCII for lowercase letters are consecutive integers starting from the code for 'a', then for 'b', 'c', . . . , and 'z'. The same is true for the uppercase letters. Furthermore, the ASCII code for 'a' is greater than the code for 'A'. So 'a' - 'A' is the same as 'b' - 'B'. For a lowercase letter ch, its corresponding uppercase letter is static_cast<char>('A' + (ch - 'a')). Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 49

Example: Computing Loan Payments This program lets the user enter the interest rate, number

Example: Computing Loan Payments This program lets the user enter the interest rate, number of years, and loan amount and computes monthly payment and total payment. Compute. Loan Run Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 50

Example: Monetary Units This program lets the user enter the amount in decimal representing

Example: Monetary Units This program lets the user enter the amount in decimal representing dollars and cents and output a report listing the monetary equivalent in single dollars, quarters (25), dimes(10), nickels(5), and pennies. Your program should report maximum number of dollars, then the maximum number of quarters, and so on, in this order. Compute. Change Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X Run 51

Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount *

Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount * 100); // Find the number of one dollars int number. Of. One. Dollars = remaining. Amount / 100; remaining. Amount = remaining. Amount % 100; remaining. Amount 1156 remaining. Amount initialized // Find the number of quarters in the remaining amount int number. Of. Quarters = remaining. Amount / 25; remaining. Amount = remaining. Amount % 25; // Find the number of dimes in the remaining amount int number. Of. Dimes = remaining. Amount / 10; remaining. Amount = remaining. Amount % 10; // Find the number of nickels in the remaining amount int number. Of. Nickels = remaining. Amount / 5; remaining. Amount = remaining. Amount % 5; // Find the number of pennies in the remaining amount int number. Of. Pennies = remaining. Amount; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 52

animation Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount

animation Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount * 100); remaining. Amount // Find the number of one dollars int number. Of. One. Dollars = remaining. Amount / 100; remaining. Amount = remaining. Amount % 100; number. Of. One. Dollars // Find the number of quarters in the remaining amount int number. Of. Quarters = remaining. Amount / 25; remaining. Amount = remaining. Amount % 25; 1156 11 number. Of. One. Dollars assigned // Find the number of dimes in the remaining amount int number. Of. Dimes = remaining. Amount / 10; remaining. Amount = remaining. Amount % 10; // Find the number of nickels in the remaining amount int number. Of. Nickels = remaining. Amount / 5; remaining. Amount = remaining. Amount % 5; // Find the number of pennies in the remaining amount int number. Of. Pennies = remaining. Amount; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 53

animation Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount

animation Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount * 100); remaining. Amount 56 // Find the number of one dollars int number. Of. One. Dollars = remaining. Amount / 100; remaining. Amount = remaining. Amount % 100; number. Of. One. Dollars 11 // Find the number of quarters in the remaining amount int number. Of. Quarters = remaining. Amount / 25; remaining. Amount = remaining. Amount % 25; remaining. Amount updated // Find the number of dimes in the remaining amount int number. Of. Dimes = remaining. Amount / 10; remaining. Amount = remaining. Amount % 10; // Find the number of nickels in the remaining amount int number. Of. Nickels = remaining. Amount / 5; remaining. Amount = remaining. Amount % 5; // Find the number of pennies in the remaining amount int number. Of. Pennies = remaining. Amount; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 54

animation Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount

animation Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount * 100); remaining. Amount 56 // Find the number of one dollars int number. Of. One. Dollars = remaining. Amount / 100; remaining. Amount = remaining. Amount % 100; number. Of. One. Dollars 11 // Find the number of quarters in the remaining amount int number. Of. Quarters = remaining. Amount / 25; remaining. Amount = remaining. Amount % 25; number. Of. One. Quarters 2 // Find the number of dimes in the remaining amount int number. Of. Dimes = remaining. Amount / 10; remaining. Amount = remaining. Amount % 10; number. Of. One. Quarters assigned // Find the number of nickels in the remaining amount int number. Of. Nickels = remaining. Amount / 5; remaining. Amount = remaining. Amount % 5; // Find the number of pennies in the remaining amount int number. Of. Pennies = remaining. Amount; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 55

animation Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount

animation Trace Compute. Change Suppose amount is 11. 56 int remaining. Amount = (int)(amount * 100); remaining. Amount 6 // Find the number of one dollars int number. Of. One. Dollars = remaining. Amount / 100; remaining. Amount = remaining. Amount % 100; number. Of. One. Dollars 11 // Find the number of quarters in the remaining amount int number. Of. Quarters = remaining. Amount / 25; remaining. Amount = remaining. Amount % 25; number. Of. Quarters // Find the number of dimes in the remaining amount int number. Of. Dimes = remaining. Amount / 10; remaining. Amount = remaining. Amount % 10; 2 remaining. Amount updated // Find the number of nickels in the remaining amount int number. Of. Nickels = remaining. Amount / 5; remaining. Amount = remaining. Amount % 5; // Find the number of pennies in the remaining amount int number. Of. Pennies = remaining. Amount; Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 56

Example: Displaying Current Time Write a program that displays current time in GMT in

Example: Displaying Current Time Write a program that displays current time in GMT in the format hour: minute: second such as 1: 45: 19. The time(0) function in the ctime header file returns the current time in seconds elapsed since the time 00: 00 on January 1, 1970 GMT, as shown in Figure 2. 1. This time is known as the Unix epoch because 1970 was the year when the Unix operating system was formally introduced. Show. Current. Time Run Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 57

Programming Style and Documentation F Appropriate Comments F Naming Conventions F Proper Indentation and

Programming Style and Documentation F Appropriate Comments F Naming Conventions F Proper Indentation and Spacing Lines F Block Styles Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 58

Programming Errors F Syntax error F Runtime error F Logic error F Debugging (for

Programming Errors F Syntax error F Runtime error F Logic error F Debugging (for logical errors) Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 59

Assignment F Review questions: 1, 3, 5, 8, 9, 16, 17. F Programming Exercises:

Assignment F Review questions: 1, 3, 5, 8, 9, 16, 17. F Programming Exercises: 3, 6. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 60