chapter 7 simple date types dr. jiung-yao huang dept. comm. eng. nat. chung cheng univ. e-mail :...

42
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : [email protected] TA: 鄭鄭鄭 鄭鄭鄭

Post on 19-Dec-2015

241 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

Chapter 7Simple Date Types

Dr. Jiung-yao HuangDept. Comm. Eng.Nat. Chung Cheng Univ.E-mail : [email protected]: 鄭筱親 陳昱豪

Page 2: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-2中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

本章重點

Enumerated typeDeclaring a function parameterBisection method

Page 3: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-3中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

outline

7.1 REPRESENTATION AND CONVERSION OF NUMERIC TYPES

7.2 REPRESENTATION AND CONVERSION OF TYPE CHAR

7.3 ENUMERATED TYPES7.4 ITERATIVE APPROXIMATIONS

CASE STUDY: BISECTION METHOD FOR FINDING ROOTS

7.5 COMMON PROGRAMMING ERRORS

Page 4: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-4中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 5: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-5中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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: integerreal number: mantissa x 2exponent

Page 6: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-6中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

%e : print DBL_MIN, DBL_MAX in scientific notation

p.805, limits.h, float.h

Page 7: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-7中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

7.1 (cont) Integer Types in C

Type Range in Typical Microprocessor Implementation

short -32767 ~ 32767

unsigned short 0 ~ 65535

int -32767 ~ 32767

unsigned 0 ~ 65535

long -2147483647 ~ 2147483647

unsigned long 0 ~ 4294967295

Page 8: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-8中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

7.1 (cont) Floating-Point Types in C

Type Approximate Range*

Significant

Digits*

float 10-37 ~1038 6

double 10-307 ~10308 15

long double 10-4931 ~104932 19

Page 9: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-9中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 10: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-10中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 11: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-11中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

7.1 (cont) Automatic Conversion of Data Types

variable initialized int k = 5, m = 4, n; double x = 1.5, y = 2.1, z;

Page 12: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-12中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

7.1 (cont) Automatic Conversion of Data Types

Context of Conversion Example Explanation

Expression with binary operator and operands of different numeric types

k + x

value is 6.5

Value of int k is converted to type double

Assignment statement with type double target variable and type int expression

z = k / m;

expression value is 1; value assigned

to z is 1.0

Expression is evaluated first. The result is converted to type double

Assignment statement with type int target variable and type double expression

n = x * y;

expression value is 3.15; value

assigned to n is 3

Expression is evaluated first. The result is converted to type int

Page 13: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-13中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

7.1 (cont) Explicit Conversion of Data Types

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)

p.63 Table 2.9

Page 14: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-14中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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’) …

Page 15: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-15中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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’

Page 16: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-16中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 17: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-17中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 7.3 Program to Print Part of the Collating Sequence

Page 18: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-18中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

7.3 Enumerated Types

Enumerated type A data type whose list of values is specified by

the programmer in a type declarationEnumeration 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.

Page 19: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-19中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 7.4 Enumerated Type for Budget Expenses

Page 20: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-20中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 21: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-21中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 22: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-22中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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;

Page 23: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-23中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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.

Page 24: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-24中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 7.5 Accumulating Weekday Hours Worked

Page 25: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-25中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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.

Page 26: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-26中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 27: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-27中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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.

Page 28: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-28中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Call to evaluate Output Produced

evaluate(sqrt, 0.25, 25.0, 100.0)

f(0.25000)=0.50000

f(25.00000)=5.00000

f(100.00000)=10.00000

evaluate(sin, 0.0, 3.14156, 0.5*3.14156)

f(0.00000)=0.00000

f(3.14159)=0.00000

f(1.57079)=1.00000

Page 29: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-29中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

xmid =xleft + xright

2.0

Page 30: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-30中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Analysis Problem Inputs

double x_leftdouble x_rightdouble epsilondouble f(double farg)

Problem Outputsdouble rootint *errp

Page 31: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-31中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 7.8 Change of Sign Implies an Odd Number of Roots

Page 32: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-32中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 33: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-33中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 34: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-34中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Design Program variables

int root_founddouble x_middouble f_left, f_mid, f_right

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

Page 35: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-35中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Design Refinement

5.1 while x_right – x_left > epsilon and !root_found8.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)

Page 36: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-36中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 7.10 Finding a Function Root Using the Bisection Method

Page 37: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-37中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 38: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-38中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 39: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-39中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 7.11 Sample Run of Bisection Program with Trace Code Included

Page 40: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-40中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 41: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-41中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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.

Page 42: Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

3-42中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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.