1 / 25

Chapter 7: Simple Data Types By: Suraya Alias

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.

grazia
Télécharger la présentation

Chapter 7: Simple Data Types By: Suraya Alias

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7:Simple Data Types By: Suraya Alias

  2. 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.

  3. 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

  4. Figure 7.2 Program to Print Implementation-Specific Ranges for Positive Numeric Data

  5. 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

  6. 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

  7. Figure 7.3 Program to Print Part of the Collating Sequence Collating sequences – a sequence of characters arranged by Character code number

  8. 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).

  9. 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.

  10. Figure 7.4 Enumerated Type for Budget Expenses

  11. Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

  12. Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

  13. Figure 7.5 Accumulating Weekday Hours Worked

  14. Figure 7.5 Accumulating Weekday Hours Worked (cont’d)

  15. Figure 7.6 Six Roots for the Equation f(x) = 0

  16. Figure 7.7 Using a Function Parameter

  17. Figure 7.8 Change of Sign Implies an Odd Number of Roots

  18. Figure 7.9Three PossibilitiesThat Arise When the Interval [xleft, xright] Is Bisected

  19. Figure 7.10 Finding a Function Root Using the Bisection Method

  20. Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

  21. Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

  22. Figure 7.11 Sample Run of Bisection Program with Trace Code Included

  23. Figure 7.12 Geometric Interpretation of Newton's Method

  24. Figure 7.13 Approximating the Area Under a Curve with Trapezoids

  25. Figure 7.14 Finite State Machine for Numbers and Identifiers

More Related