Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple Data Types Chapter 7. 2 7.1 Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.

Similar presentations


Presentation on theme: "Simple Data Types Chapter 7. 2 7.1 Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value."— Presentation transcript:

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 Due 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 (in the text) 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 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

11 11 7.3 Character Data and Functions t Character literal const char star = ‘*’; char nextLetter; nextLetter = ‘A’;

12 12 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

13 13 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

14 14 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

15 15 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;

16 16 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; }

17 17 collate.cpp Program Output Program output … !”#$%&`()*+,./0123456789;: ?ABCDEFG HIJKLMNOPQRSTUVWXYZ[/]^_’abcdef ghijklmnopqrstuvwxyz{|}~.

18 18 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 To displaying bool values (true/false) put the following statements –cin.setf (ios::boolalpha); // before reading –cout.setf (ios::boolalpha); // before writing

19 19 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

20 20 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)

21 21 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

22 22 color.cpp // DISPLAYS THE VALUE OF thisColor void writeColor (color thisColor) { switch (thisColor) { case red: cout << "red"; break; case green: cout << "green"; break;

23 23 color.cpp case blue: cout << "blue"; break; case yellow: cout << "yellow"; break; default: status = 0; cerr << "*** ERROR: Invalid color value." << endl; }

24 24 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

25 25 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 (no numbers, strings or characters) –Only use one value for each enumerated declaration –Integers and Enumeration  Wrong: myColor = i; // where i an integer  Correct: myColor = color(i); //casting and use assuming I is not of range


Download ppt "Simple Data Types Chapter 7. 2 7.1 Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value."

Similar presentations


Ads by Google