Presentation is loading. Please wait.

Presentation is loading. Please wait.

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program.

Similar presentations


Presentation on theme: "Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program."— Presentation transcript:

1 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program literals Binary and hexadecimal representation for integers Declare and initialize variables in program How calculations using integers work Floating-point calculations Constant and variables

2 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Data and data types Integer literals Floating point literals Binary literals  0, 1 Octal laterals  0, …, 7 Decimal digit  0, …, 9 Hexadecimal digit  0, …, 9, A, B, C, D, E, F

3 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Storage Unit 1.One byte = 8 bits 2.1 Kilo Byte (KB) = 1024 Bytes 3.1 Mega Byte (MB) = 1024 KBs 4.1 Giga Byte (GB) = 1024 MBs 5.1 Tera Byte (TB) = 1024 GBs = 2 10 GBs 6.1 Peta Byte (PB) = 1024 TBs 7.1 Exa Byte (EB) = 1024 PBs = 2 60 Bytes

4 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Number system Binary system – 0, 1 Octal system – 0,1,2,3,4,5,6,7 –Leading by ‘0’, such as 0256 Hexadecimal system –0,1,,2,3,4,5,6,7,8,9,A,B,C,D,E,F –Leading by ‘0x’ or ‘0X’, such as 0X1D3C Decimal system –0,1,2,3,4,5,6,7,8,9

5 Number interchange to decimal Binary to decimal –(10110.011) 2 =16+4+2+0.25+0.125=(26.375) 10 Octal to decimal –(524.4) 8 =5*64+2*8+4+4*0.125=(340.5) 10 Hexadecimal to decimal –(C2A.D) 16 =12*256+2*16+10+13*0.0625=(3114.812 5) 10

6 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Number interchange (59.75) 10 = (111011.11) 2 = (3B.C) 16 = (73.6) 8 (25.4) 10 = (11001.011001100110…) 2 = (31.3146314631463146…) 8 = (19.6666…) 16

7 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Binary Operation (0000 1100) + (0110 1101) = (0111 1001) 1’s complement of (0000 1100) → (1111 0011) 2’s complement of (0000 1100) → (1111 0100) = 1’s complement + 1 (1100 1011) – (0100 1100) = (1100 1011) + (1011 0100) = (0111 1111)

8 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Variable Types Integer variables –char (1byte) –short (2 bytes) –int (4 bytes) –long (4 or 8 bytes) Floating variables –float (4 bytes) –double (8 bytes) –long double (8 or 10 bytes)

9 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Integer Domain in VC++ TypeSize (bytes)Domain char1-128 ~ 127 unsigned char10U ~ 255U short2-32768 ~ 32767 unsigned short20U ~ 65535U int4-2 31 ~ 2 31 -1 unsigned int40U ~ 2 32 -1U long4-2 31 L~ 2 31 -1L unsigned long40UL ~ 2 32 -1UL

10 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Numeric Operators Plus, positive  + Minus, negative  - Multiply  * Divide  / Modulus  % Increment  ++ Decrement  -- Assignment operators  =, +=, -=, *=, /=, %=, &=, |=, ^=, >= Programs 2.1, 2.1A, 2.2, 2.3

11 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

12 op= Assignment Operator apples *= oranges + 2; apples = apples * (oranges + 2); oranges += 2; sum += i++; // sum +=i; i=i+1; sum += ++i; // i = i+1; sum +=i; total=++count+6; // count++; total=count+6; total=count++ +6; // total=count+6; count++;

13 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The const keyword Constant –Magic numbers, such as PI, exponential number, unit, etc. –Values or contents that do not or cannot change or vary Program 2.4

14 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Numeric functions for integers #include “math.h” or #include using namespace std;

15 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Random numbers #include // standard library #include // timing function // random seed Program 2.5

16 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating numbers In VC++ “-1.2345e003” represent the above number

17 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating-point data types TypeSize (bytes) Decimal precision Domain float46≒ 1.2x10 -38 ~ 3.4x10 38 double815≒ 2.2x10 -308 ~ 1.8x10 308 long double 8 (10)15 (19)≒ 2.2x10 -308 ~ 1.8x10 308 (3.3x10 -4096 ~ 1.2x10 4096 )

18 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating Operation Program 2.6 The float.h file Program 2.7 –Floating point error –Never rely on an exact floating-point representation of a decimal value in your program code Program 2.8 –Floating point output manupilator

19 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

20

21 Escape sequences

22 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Character Literals Program 2.9 App. A ASCII code Extended character set –ASCII code 128 ~ 255 –Wide character 2 or 4 bytes character (wchar_t) Uicode defined international character set

23 Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Initialization int unlucky = 13; is same as int unlucky(13); char letter(‘A’); is same as char letter = 65;


Download ppt "Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program."

Similar presentations


Ads by Google