chapter 7: simple data types by: suraya alias

25
Chapter 7: Simple Data Types By: Suraya Alias 1-1

Upload: grazia

Post on 23-Feb-2016

73 views

Category:

Documents


0 download

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

TRANSCRIPT

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

Chapter 7:Simple Data Types

By: Suraya Alias

1-1

Page 2: Chapter  7: Simple Data Types By:  Suraya  Alias

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

Page 3: Chapter  7: Simple Data Types By:  Suraya  Alias

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

Page 4: Chapter  7: Simple Data Types By:  Suraya  Alias

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

1-4

Page 5: Chapter  7: Simple Data Types By:  Suraya  Alias

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

Page 6: Chapter  7: Simple Data Types By:  Suraya  Alias

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

Page 7: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.3 Program to Print Part of the Collating Sequence

1-7

Collating sequences – a sequence of characters arranged byCharacter code number

Page 8: Chapter  7: Simple Data Types By:  Suraya  Alias

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

Page 9: Chapter  7: Simple Data Types By:  Suraya  Alias

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

Page 10: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.4 Enumerated Type for Budget Expenses

1-10

Page 11: Chapter  7: Simple Data Types By:  Suraya  Alias

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

1-11

Page 12: Chapter  7: Simple Data Types By:  Suraya  Alias

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

1-12

Page 13: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.5 Accumulating Weekday Hours Worked

1-13

Page 14: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.5 Accumulating Weekday Hours Worked (cont’d)

1-14

Page 15: Chapter  7: Simple Data Types By:  Suraya  Alias

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

1-15

Page 16: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.7 Using a Function Parameter

1-16

Page 17: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.8 Change of Sign Implies an Odd Number of Roots

1-17

Page 18: Chapter  7: Simple Data Types By:  Suraya  Alias

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

1-18

Page 19: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.10 Finding a Function Root Using the Bisection Method

1-19

Page 20: Chapter  7: Simple Data Types By:  Suraya  Alias

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

1-20

Page 21: Chapter  7: Simple Data Types By:  Suraya  Alias

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

1-21

Page 22: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.11 Sample Run of Bisection Program with Trace Code Included

1-22

Page 23: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.12 Geometric Interpretation of Newton's Method

1-23

Page 24: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.13 Approximating the Area Under a Curve with Trapezoids

1-24

Page 25: Chapter  7: Simple Data Types By:  Suraya  Alias

Figure 7.14 Finite State Machine for Numbers and Identifiers

1-25