Download presentation
Presentation is loading. Please wait.
Published byEthelbert Hardy Modified over 8 years ago
1
Simple Data Types Chapter 7
2
2 7.1 Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value –Programming practices const type identifier = constant;
3
3 #define t An additional way to define constants t Used in older C programs prior to introduction of constants #define identifier replacement-text #define pi 3.14159 The same as const float pi = 3.14159;
4
4 7.2 Internal Representations of int and float t float and int used to represent numbers t Stored differently in the computer t int stored as binary 1s and 0s –sign and binary number t float stored in 2 parts plus the sign –sign - characteristic - mantissa –type float number = 2^characteristic * mantissa
5
5 Value Variations t Three sizes of int –short int –int –long int t Each uses a different amount of the computers memory –long and short provide consistency between compilers –short can save lots of memory
6
6 Value Variations t Three sizes of float –float –double –long double t Each uses a different amount of the computers memory –double is no less precise than float –long double provides less precision than double
7
7 Numerical Inaccuracies t Can have errors when using float in some computations t Do to the way floats are stored t Errors will be determined by the number of binary bits used in the mantissa t Arithmetic underflow and arithmetic overflow –Multiplying 2 small or large numbers together respectively
8
8 Ranges for int and float Constants t See table 7.1 t Definitions for some of these C++ constants are in the limits.h and float.h libraries t The actual values of these constants will vary from computer to computer
9
9 Mixing Types t The notion of type promotion –promoting a lower type to a higher type example: 3 + x /2 –if x is float constant would be promoted to float as well and actually be 2.0 t Type conversions –int to float (number.0) –float to int (truncation occurs)
10
10 Mixing Types t Example: int y; float x = 3.89; y = x; y would contain 3
11
11 Type Casting t Avoid mixing types but if you need to you can cast a type t Type casting allows you to change a type within the program for a specific function form: type (variable) average = sum / float (n); where n is declared as an int
12
12 7.3 Character Data and Functions t Character literal const char star = ‘*’; char nextLetter; nextLetter = ‘A’;
13
13 Character Representation t Bits required to store characters is based on the ASCII table (Appendix A) t Each character has an numeric code t 1 byte or 8 bits are typically used to represent characters
14
14 Relational Operators and Characters t Relational operators can be used with characters t Testing based on the characters ASCII value example:‘0’ < ‘1’ True ‘A’ < ‘B’ True
15
15 Character Functions t ctype.h library provides many functions to the programmer t Table 7.2 lists many of the functions t Function name on the left and its purpose is listed to the right tolower (c) if c is uppercase, this function returns the corresponding lower case letter
16
16 collate.cpp // FILE: collate.cpp // PRINTS PART OF THE CHARACTER COLLATING // SEQUENCE #include using namespace std; int main () { const int MIN = 32; const int MAX = 126; char nextChar;
17
17 collate.cpp // Display sequence of characters. for (int nextCode = MIN; nextCode <= MAX; nextCode++) { nextChar = char (nextCode); cout << nextChar; if (nextChar == 'Z') cout << endl; } return 0; }
18
18 collate.cpp Program Output Program output … !”#$%&`()*+,./0123456789;: ?ABCDEFG HIJKLMNOPQRSTUVWXYZ[/]^_’abcdef ghijklmnopqrstuvwxyz{|}~.
19
19 7.4 Type bool Data and Logical Expressions t Used in assignment statements and logical expressions (True and False) t Complementing expressions <>= ><= >=< ==!= !===
20
20 Type bool Functions t bool function isdigit if (isdigit (ch)) cout << “You entered a number”; t bool return value from a function if (centsOverflow (cents)) { cents -= 100; dollars ++; }
21
21 Input and Output with bool t Can NOT be used for input or output –True represented by a numeric 1 –False represented by numeric 0 t Displaying bool values –cin.setf (ios::boolalpha); –cout.setf (ios::boolalpha);
22
22 7.5 Enumeration Types t Aid program readability t Represent various states example: enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday}; sunday has the value 0 monday has the value 1 and so on user-defined data type
23
23 Enumeration Type Declarations enum enumeration-type {enumerator-list}; enum classId {freshman, sophomore, junior, senior}; classId newClass; if (newClass == freshman) do something else if (newClass == sophomore)
24
24 Enumeration Types t Characters t Switch statements t Comparisons t Write functions to read and write enumerated types (not know to compiler) t Discuss color.cpp
25
25 color.cpp // DISPLAYS THE VALUE OF thisColor void writeColor (color thisColor) { switch (thisColor) { case red: cout << "red"; break; case green: cout << "green"; break;
26
26 color.cpp case blue: cout << "blue"; break; case yellow: cout << "yellow"; break; default: status = 0; cerr << "*** ERROR: Invalid color value." << endl; }
27
27 7.6 Common Programming Errors t Omitting pairs of parentheses –m = y2 - y1 / x2 - x1 –Compiler will not complain but calculation will be in error t Unbalanced parentheses –z = sqrt (x + y) / (1 + sqrt (x + y)); t Mixing operators and operand types –float == char
28
28 Common Programming Errors t Operator Precedence errors –Watch use of parentheses to get correct precedence –! Symbol t Enumeration types –Identifiers can only appear in list –Only use one value for each enumerated declaration
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.