1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations
2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 2 Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area of the circle. ComputeAreaRun
3 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 3 Trace a Program Execution #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; } no value radius allocate memory for radius animation
4 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 4 Trace a Program Execution no value radius memory animation #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; } no value area allocate memory for area
5 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 5 Trace a Program Execution 20 radius no value area assign 20 to radius animation #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; }
6 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 6 Trace a Program Execution 20 radius memory area compute area and assign it to variable area animation #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; }
7 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 7 Trace a Program Execution 20 radius memory area print a message to the console animation #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; }
8 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 8 Reading Input from the Keyboard You can use the std::cin object to read input from the keyboard. ComputeArea1Run
9 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 9 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.
10 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 10 Variables // Compute the first area radius = 1.0; area = radius * radius * ; std::cout << area; // Compute the second area radius = 2.0; area = radius * radius * ; std::cout << area;
11 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 11 Declaring Variables int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable;
12 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 12 Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a;
13 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 13 Declaring and Initializing in One Step F int x = 1; F double d = 1.4;
14 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 14 Named Constants const datatype CONSTANTNAME = VALUE; const double PI = ; const int SIZE = 3;
15 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 15 Numerical Data Types
16 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 16 Numeric Literals A literal is a constant value that appears directly in a program. For example, 34, , and 5.0 are literals in the following statements: int i = 34; long k = ; double d = 5.0;
17 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 17 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 e+1 is converted into scientific notation such as , its decimal point is moved (i.e., floated) to a new position.
18 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 18 Numeric Operators
19 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 19 Integer Division +, -, *, /, and % 5 / 2 yields an integer / 2 yields a double value % 2 yields 1 (the remainder of the division)
20 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 20 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 you 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:
21 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 21 Example: Displaying Time Write a program that obtains hours and minutes from seconds. DisplayTimeRun
22 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 22 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 is too large. short value = ;
23 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 23 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 is too small. short value = ;
24 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 24 Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
25 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 25 Example: Converting Temperatures Write a program that converts a Fahrenheit degree to Celsius using the formula: FahrenheitToCelsius Run
26 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 26 Shorthand Assignment Operators OperatorExampleEquivalent +=i += 8i = i + 8 -=f -= 8.0f = f *=i *= 8i = i * 8 /=i /= 8i = i / 8 %=i %= 8i = i % 8
27 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 27 Increment and Decrement Operators OperatorNameDescription ++varpreincrementThe expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++postincrementThe expression (var++) evaluates to the original value in var and increments var by 1. --varpredecrementThe expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. var--postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.
28 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 28 Increment and Decrement Operators, cont.
29 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 29 Character Data Type char letter = 'A'; (ASCII) char numChar = '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;
30 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 30 Read Characters To read a character from the keyboard, use cout << "Enter a character: "; char ch; cin >> ch;
31 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 31 Escape Sequences for Special Characters
32 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 32 Appendix B: ASCII Character Set ASCII Character Set is a subset of the Unicode from \u0000 to \u007f
33 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 33 ASCII Character Set, cont. ASCII Character Set is a subset of the Unicode from \u0000 to \u007f
34 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 34 Casting between char and Numeric Types int i = ' a ' ; // Same as int i = (int) ' a ' ; char c = 97; // Same as char c = (char)97;
35 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 35 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, dimes, nickels, and pennies. Your program should report maximum number of dollars, then the maximum number of quarters, and so on, in this order. ComputeChangeRun
36 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 36 Trace ComputeChange int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount; 1156 remainingAmount remainingAmount initialized Suppose amount is 11.56
37 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 37 Trace ComputeChange int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount; 1156 remainingAmount Suppose amount is numberOfOneDollars numberOfOneDollars assigned animation
38 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 38 Trace ComputeChange int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount; 56 remainingAmount Suppose amount is numberOfOneDollars remainingAmount updated animation
39 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 39 Trace ComputeChange int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount; 56 remainingAmount Suppose amount is numberOfOneDollars 2 numberOfOneQuarters numberOfOneQuarters assigned animation
40 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 40 Trace ComputeChange int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount; 6 remainingAmount Suppose amount is numberOfOneDollars 2 numberOfQuarters remainingAmount updated animation
41 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 41 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: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. ShowCurrentTimeRun
42 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 42 Programming Style and Documentation F Appropriate Comments F Naming Conventions F Proper Indentation and Spacing Lines F Block Styles F 122 Style Guide 122 Style Guide