Chapter 2 Elementary Programming Liang Introduction to Programming

  • Slides: 29
Download presentation
Chapter 2 Elementary Programming Liang, Introduction to Programming with C++, Second Edition, (c) 2010

Chapter 2 Elementary Programming Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1

Numerical Data Types Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson

Numerical Data Types Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 3

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. 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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5

octal and hex literals To denote an octal integer literal, use a leading 0

octal and hex literals 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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6

Companion Website Hexadecimal Binary Hex Decimal 0000 0001 0010 0011 0100 0101 0110 0111

Companion Website Hexadecimal Binary Hex Decimal 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 To convert a hexadecimal number to a binary number, simply convert each digit in the hexadecimal number into a four-digit binary number. To convert a binary number to a hexadecimal, convert every four binary digits from right to left in the binary number into a hexadecimal number. For example, Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8

Numeric Operators Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education,

Numeric Operators Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11

Example: Displaying Time Write a program that obtains minutes and remaining seconds from an

Example: Displaying Time Write a program that obtains minutes and remaining seconds from an amount of time in seconds. #include <iostream> using namespace std; Display. Time. cpp int main() { int seconds = 500; int minutes = seconds / 60; int remaining. Seconds = seconds % 60; cout << seconds << " seconds is " << minutes << " minutes and " << remaining. Seconds << " seconds " << endl; system("PAUSE"); return 0; 500 seconds is 8 minutes and 20 seconds } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13

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: #include <iostream> using namespace std; Fahrenhet. To. Celsius. cpp int main() { double fahrenheit; cout << "Enter a degree in Fahrenheit: "; cin >> fahrenheit; // Enter a degree in Fahrenheit // Obtain a celsius degree double celsius = (5. 0 / 9) * (fahrenheit - 32); cout << "Fahrenheit " << fahrenheit << " is " << celsius << " in Celsius" << endl; // Display result system("PAUSE"); return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 15

Increment and Decrement Operators Operator ++var Name preincrement Description The expression (++var) increments var

Increment and Decrement Operators Operator ++var Name preincrement Description The expression (++var) increments var by 1 , and the new value of var is returned. var++ postincrement The expression (var++) returns the original old value , and then var is incremented by 1. --var predecrement The expression (--var) decrements var by 1 , and the new value of var is returned. var-- postdecrement The expression (var--) returns the original old value, and then var is decremented by 1. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 16

Increment and Decrement Operators, cont. new. Num becomes 100 new. Num becomes 110 Liang,

Increment and Decrement Operators, cont. new. Num becomes 100 new. Num becomes 110 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 17

Increment and Decrement Operators, cont. double x=1. 0; double y=5. 0; double z=x-- +

Increment and Decrement Operators, cont. double x=1. 0; double y=5. 0; double z=x-- + (++y) After all three lines are executed, y becomes 6. 0 z becomes 7. 0 x becomes 0. 0 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 18

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 19

Type Casting. Converting an expression of a given type into another type. double d

Type Casting. Converting an expression of a given type into another type. double d = 3; (widening) -------------------int i = static_cast<int>(5. 4); (narrowing) This is the same as: int i = (int)5. 4; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 20

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 21

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. #include <iostream> using namespace std; If user entered 197. 55 The sales tax is 6% of purchase Tax is 11. 853 int main() { Casting statement: // Enter purchase amount static_cast<int>(tax * 100) gives 1185 double purchase. Amount; So: cout << "Enter purchase amount: "; static_cast<int>(tax*100)/100. 0 is 11. 85 cin >> purchase. Amount; Sales. Tax. cpp double tax = purchase. Amount * 0. 06; cout << "Sales tax is " << static_cast<int>(tax * 100) / 100. 0; system("PAUSE"); return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 22

Character Data Type The character type, char , is used to represent a single

Character Data Type The character type, char , is used to represent a single letter. 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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 23

Appendix B: ASCII Character Set ASCII (American Standard Code for Information Interchange) The size

Appendix B: ASCII Character Set ASCII (American Standard Code for Information Interchange) The size of the char type is : 1 byte. Example: the letter A has decimal equivalent 65. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 24

ASCII Character Set, cont. ASCII Character Set in the hexadecimal index. Liang, Introduction to

ASCII Character Set, cont. ASCII Character Set in the hexadecimal index. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 25

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 26

Escape Sequences for Special Characters Example: the following statement displays a text and moves

Escape Sequences for Special Characters Example: the following statement displays a text and moves the cursor to the next line: cout<<“Welcome to C++n”; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 27

Casting between char and Numeric Types int i = 'a'; // Same as int

Casting between char and Numeric Types int i = 'a'; // Same as int i = (int)'a'; cout<<i; // i is 97; char ch = 97; //Same as char ch=(char)97; Cout<<ch; // ch is a; Char ch = 65. 25; Cout << ch; // ch is character A; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 28

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 Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 29