Upload
grazia
View
80
Download
0
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
Chapter 7:Simple Data Types
By: Suraya Alias
1-1
7.1 Representation and Conversion of Numeric TypesNo 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.
1-2
Figure 7.1 Internal Formats of Type int and Type double
1-3
Differences between Numeric TypesExample : Integer 13 is represented by binary number 01101But for type double is given by the formula: where mantissa isA binary fraction between 0.5 and 1.0 for positive number and -0.5 and -1.0 for negative number
exponent2 X mantissanumber real
Figure 7.2 Program to Print Implementation-Specific Ranges for Positive Numeric Data
1-4
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
1-5
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
1-6
Figure 7.3 Program to Print Part of the Collating Sequence
1-7
Collating sequences – a sequence of characters arranged byCharacter 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_day has 7 possible values.typedef enum {Monday, Tuesday, Wednesday, Thursday,Friday,Saturday, Sunday}week_day;
◦ The new type name week_day is 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 defining week_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).
1-8
Enumerated Type DefinitionSyntax: typedef enum
{identifier list}enum type;
example:typedef enum{black, white, red, blue, green}color;
The first identifier is represented by integer 0, so black is 0.
1-9
Figure 7.4 Enumerated Type for Budget Expenses
1-10
Figure 7.4 Enumerated Type for Budget Expenses (cont’d)
1-11
Figure 7.4 Enumerated Type for Budget Expenses (cont’d)
1-12
Figure 7.5 Accumulating Weekday Hours Worked
1-13
Figure 7.5 Accumulating Weekday Hours Worked (cont’d)
1-14
Figure 7.6 Six Roots for the Equation f(x) = 0
1-15
Figure 7.7 Using a Function Parameter
1-16
Figure 7.8 Change of Sign Implies an Odd Number of Roots
1-17
Figure 7.9 Three PossibilitiesThat Arise When the Interval [xleft, xright] Is Bisected
1-18
Figure 7.10 Finding a Function Root Using the Bisection Method
1-19
Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)
1-20
Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)
1-21
Figure 7.11 Sample Run of Bisection Program with Trace Code Included
1-22
Figure 7.12 Geometric Interpretation of Newton's Method
1-23
Figure 7.13 Approximating the Area Under a Curve with Trapezoids
1-24
Figure 7.14 Finite State Machine for Numbers and Identifiers
1-25