Chapter 7
This presentation is the property of its rightful owner.
Sponsored Links
1 / 42

Chapter 7 PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 7. Simple Date Types. Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : [email protected] TA: 鄭筱親 陳昱豪. 本章重點. Enumerated type Declaring a function parameter Bisection method. outline. 7.1 REPRESENTATION AND CONVERSION OF NUMERIC TYPES

Download Presentation

Chapter 7

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 7

Chapter 7

Simple Date Types

Dr. Jiung-yao Huang

Dept. Comm. Eng.

Nat. Chung Cheng Univ.

E-mail : [email protected]

TA: 鄭筱親 陳昱豪


Chapter 7

本章重點

  • Enumerated type

  • Declaring a function parameter

  • Bisection method


Outline

outline

  • 7.1 REPRESENTATION AND CONVERSION OF NUMERIC TYPES

  • 7.2 REPRESENTATION AND CONVERSION OF TYPE CHAR

  • 7.3 ENUMERATED TYPES

  • 7.4 ITERATIVE APPROXIMATIONS

    • CASE STUDY: BISECTION METHOD FOR FINDING ROOTS

  • 7.5 COMMON PROGRAMMING ERRORS


7 1 representation and conversion of numeric types

7.1 Representation and Conversion of Numeric Types

  • Simple data type

    • A data type used to store a single value

    • Uses a single memory cell to store a variable

  • Different numeric types has different binary strings representation in memory


Figure 7 1 internal formats of type int and type double

Figure 7.1 Internal Formats of Type int and Type double

mantissa: binary fraction between

0.5~1.0 for positive numbers

-0.5~-1.0 for negative numbers

exponent: integer

real number: mantissa x 2exponent


Figure 7 2 program to print implementation specific ranges for positive numeric data

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

p.805, limits.h, float.h

%e : print DBL_MIN, DBL_MAX in scientific notation


7 1 cont integer types in c

7.1 (cont) Integer Types in C


7 1 cont floating point types in c

7.1 (cont) Floating-Point Types in C


7 1 cont numerical inaccuracies

7.1 (cont) Numerical Inaccuracies

  • Representational error (round-off error)

    • An error due to coding a real number as a finite number of binary digits

  • Cancellation error

    • An error resulting from applying an arithmetic operation to operands of vastly different magnitudes; effect of smaller operand is lost


7 1 cont numerical inaccuracies1

7.1 (cont) Numerical Inaccuracies

  • Arithmetic underflow

    • An error in which a very small computational result is represented as zero

  • Arithmetic overflow

    • An error that is an attempt to represent a computational result that is too large


7 1 cont automatic conversion of data types

7.1 (cont) Automatic Conversion of Data Types

  • variable initialized

    • int k = 5, m = 4, n;

    • double x = 1.5, y = 2.1, z;


7 1 cont automatic conversion of data types1

7.1 (cont) Automatic Conversion of Data Types


7 1 cont explicit conversion of data types

7.1 (cont) Explicit Conversion of Data Types

p.63 Table 2.9

  • cast

    • an explicit type conversion operation

    • not change what is stored in the variable

  • Ex.

    • frac = (double) n1 / (double) d1;

    • Average = (double) total_score / num_students(p.63)


7 2 representation and conversion of type char

7.2 Representation and Conversion of Type char

  • A single character variable or value may appear on the right-hand side of a character assignment statement.

  • Character values may also be compared, printed, and converted to type int.

    #define star ‘*’

    char next_letter = ‘A’;

    if (next_letter < ‘Z’) …


7 2 cont three common character codes appendix a

7.2 (cont) Three Common character codes(Appendix A)

  • Digit character

    • ASCII ‘0’ ~’9’ have code value 48~57

    • ‘0’ < ‘1’ < ‘2’…….< ‘9’

  • Uppercase letters

    • ASCII ‘A’~’Z’ have code values 65~90

    • ‘A’ < ‘B’ < ‘C’……< ‘Z’

  • Lowercase letters

    • ASCII ‘a’~’z’ have code values 97~122

    • ‘a’ < ‘b’ < ‘c’…….< ‘z’


7 2 cont example 7 1

7.2 (cont) Example 7.1

  • collating sequence

    • A sequence of characters arranged by character code number

  • Fig. 7.3 uses explicit conversion of type int to type char to print part of C collating sequence


Figure 7 3 program to print part of the collating sequence

Figure 7.3 Program to Print Part of the Collating Sequence


7 3 enumerated types

7.3 Enumerated Types

  • Enumerated type

    • A data type whose list of values is specified by the programmer in a type declaration

  • Enumeration constant

    • An identifier that is one of the values of an enumerated type

  • Fig. 7.4 shows a program that scans an integer representing an expense code and calls a function that uses a switch statement to display the code meaning.


Figure 7 4 enumerated type for budget expenses

Figure 7.4 Enumerated Type for Budget Expenses


Figure 7 4 enumerated type for budget expenses cont d

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


Figure 7 4 enumerated type for budget expenses cont d1

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


7 3 cont enumerated type definition

7.3 (cont) Enumerated Type Definition

  • Syntax:

    typedef enum

    {identifier_list}

    enum_type;

  • Example:

    typedef enum

    {sunday, monday, tuesday, wednesday, thursday, friday, saturday}

    day_t;


7 3 cont example 7 3

7.3 (cont) Example 7.3

  • The for loop in Fig. 7.5 scans the hours worked each weekday for an employee and accumulates the sum of these hours in week_hours.


Figure 7 5 accumulating weekday hours worked

Figure 7.5 Accumulating Weekday Hours Worked


7 4 iterative approximations

7.4 Iterative Approximations

  • root (zero of a function)

    • A function argument value that causes the function result to be zero

  • Bisection method

    • Repeatedly generates approximate roots until a true root is discovered.


Figure 7 6 six roots for the equation f x 0

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


Figure 7 7 using a function parameter

Figure 7.7 Using a Function Parameter

  • Declaring a function parameter is accomplished by simply including a prototype of the function in the parameter list.


7 4 cont calls to function evaluate and the output produced

7.4 (cont) Calls to Function evaluate and the Output Produced


7 4 cont case study bisection method for finding roots

xleft

+ xright

xmid =

2.0

7.4 (cont) Case Study: Bisection Method for Finding Roots

  • Problem

    • Develop a function bisect that approximates a root of a function f on an interval that contains an odd number of roots.

  • Analysis


7 4 cont case study bisection method for finding roots1

7.4 (cont) Case Study: Bisection Method for Finding Roots

  • Analysis

    • Problem Inputs

      • double x_left

      • double x_right

      • double epsilon

      • double f(double farg)

    • Problem Outputs

      • double root

      • int *errp


Figure 7 8 change of sign implies an odd number of roots

Figure 7.8 Change of Sign Implies an Odd Number of Roots


Figure 7 9 three possibilities that arise when the interval xleft xright is bisected

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


7 4 cont case study bisection method for finding roots2

7.4 (cont) Case Study: Bisection Method for Finding Roots

  • Design

    • Initial Algorithm

  • 1.if the interval contains an even number of roots

  • 2.Set error flag

  • 3.Display error message

  • else

  • 4.Clear error flag

  • 5.repeat as long as interval size is greater than

  • epsilon and root is not found

  • 6.Compute the function value at the midpoint of the interval

  • 7.if the function value is zero, the midpoint is a root

  • else

  • 8.Choose the left or right half of the interval

  • in which to continue the search

  • 9.Return the midpoint of the final interval as the root


  • 7 4 cont case study bisection method for finding roots3

    7.4 (cont) Case Study: Bisection Method for Finding Roots

    • Design

      • Program variables

        • int root_found

        • double x_mid

        • double f_left, f_mid, f_right

      • Refinement

        • 1.1 f_left = f(x_left)

        • 1.2 f_right = f(x_right)

        • 1.3 if signs of f_left and f_right are the same


    7 4 cont case study bisection method for finding roots4

    7.4 (cont) Case Study: Bisection Method for Finding Roots

    • Design

      • Refinement

        • 5.1 while x_right – x_left > epsilon and !root_found

        • 8.1 if root is in left half of interval (f_left*f_mid<0.0)

          8.2 Change right end to midpoint

          else

          8.3 Change left end to midpoint

    • Implementation (Figure 7.10)


    Figure 7 10 finding a function root using the bisection method

    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 10 finding a function root using the bisection method cont d1

    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.11 Sample Run of Bisection Program with Trace Code Included


    7 5 common programming errors

    7.5 Common Programming Errors

    • Arithmetic underflow and overflow resulting from a poor choice of variable type are causes of erroneous results.

    • Programs that approximate solutions to numerical problems by repeated calculations often magnify small errors.

    • Not reuse the enumerated identifiers in another type or as a variable name

    • C does not verify the value validity in enum variables


    Chapter review 1

    Chapter Review(1)

    • Type int and double have different internal representations.

    • Arithmetic with floating-point data may not be precise, because not all real numbers can be represented exactly.

    • Type char data are represented by storing a binary code value for each symbol.

    • Defining an enumerated type requires listing the identifier that are the values of the type.


    Chapter review 2

    Chapter Review(2)

    • A variable or expression can be explicitly converted to another type by writing the new type’s name in parentheses before the value to convert.

    • A function can take another function as a parameter.

    • The bisection method is a technique for iterative approximation of a root of a function.


  • Login