250 likes | 496 Views
Chapter 7: Simple Data Types By: Suraya Alias. 7.1 Representation and Conversion of Numeric Types. No programming language can predefine all data types, C allows to create new data types. User defined enumerated types are Simple/scalar data types.
E N D
Chapter 7:Simple Data Types By: Suraya Alias
7.1 Representation and Conversion of Numeric Types • No programming language can predefine all data types, C allows to create new data types. • User defined enumerated types are Simple/scalar data types. • Simple data type is a data type used to store a single value.
Figure 7.1 Internal Formats of Type int and Type double Differences between Numeric Types Example : Integer 13 is represented by binary number 01101 But for type double is given by the formula: where mantissa is A binary fraction between 0.5 and 1.0 for positive number and -0.5 and -1.0 for negative number
Figure 7.2 Program to Print Implementation-Specific Ranges for Positive Numeric Data
Numerical Inaccuracies • Representational error (round of error) • An error due to coding a real number as a finite number of binary digitsexample : 1/3 = 0.33333 • Cancellation error • Result from applying an arithmetic operation to operands with different magnitudes, affect of smaller operand is lost • Example : 1000.0 + 0.00001234 = 10000.0 in certain computer • Arithmetic overflow • an attempt to represent a computational result that is too large • example : 1782827.99 * 8889.566 • Arithmetic underflow • where very small computational result is represented as zero • Example : 0.0001 * 0.000012
Automatic conversion of data types(refer table 7.3) • Explicit Conversion of data types • Cast – an explicit type conversion operation • When a cast operation is applied to a variable, the conversion carried out determines the value of an expression. But it does not change the what is stored in the variable. • Example: double frac = (double)n1 / (double)d1; • If int n1 = 2 and int d1 = 4, frac = 2/4 = 0.0we use type cast to prevent integer division, thusfrac = 2.0 / 4.0 = 0.5
Figure 7.3 Program to Print Part of the Collating Sequence Collating sequences – a sequence of characters arranged by Character code number
7.3 Enumerated Types • Enumerated type • Is a data type whose list of values is specified by the programmer in a type declaration • Example; the enumerated type week_dayhas 7 possible values.typedefenum {Monday, Tuesday, Wednesday, Thursday,Friday,Saturday, Sunday}week_day; • The new type name week_dayis used similar as we use data type int or double.Example: week_day today; • Enumeration constant • An identifier that is one of the values of an enumerated type • Thus after definingweek_day, Monday will represented by 0, Tuesday is 1 and so on. • The variable today can be manipulated as we would handle any integer (such as using switch).
Enumerated Type Definition • Syntax: typedefenum{identifier list}enum type;example:typedefenum{black, white, red, blue, green}color;The first identifier is represented by integer 0, so black is 0.
Figure 7.9Three PossibilitiesThat Arise When the Interval [xleft, xright] Is Bisected
Figure 7.10 Finding a Function Root Using the Bisection Method
Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)
Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)
Figure 7.11 Sample Run of Bisection Program with Trace Code Included
Figure 7.13 Approximating the Area Under a Curve with Trapezoids
Figure 7.14 Finite State Machine for Numbers and Identifiers