Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.

Slides:



Advertisements
Similar presentations
Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
C Data Types Chapter 7 And other material. Representation long (or int on linux) Two’s complement representation of value. 4 bytes used. (Where n = 32)
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
Basic Elements of C++ Chapter 2.
Simple Data Type Representation and conversion of numbers
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
chap7 Chapter 7 Simple Data Types chap7 2 Objectives No programming language can predefine all the data types that a programmer may.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Simple Data Types Problem Solving, Abstraction, and Design using.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Simple Data Types Built-In and User Defined Chapter 10.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Unit 6 Data Types and Arrays. Key Concepts Explicit and automatic conversion ASCII Enumerated types Function parameters Arrays Loops and arrays Passing.
Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter 7: Expressions and Assignment Statements
Introduction to the C Language
Chapter 4 C Program Control Part I
Elementary Programming
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
ICS103 Programming in C Lecture 3: Introduction to C (2)
The Selection Structure
Data Structures Mohammed Thajeel To the second year students
Fundamental Data Types
Basic Elements of C++ Chapter 2.
Mr. Dave Clausen La Cañada High School
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to Abstract Data Types
Simple Data Types and Function Calls
Numerical Data Types.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Approximations and Round-Off Errors Chapter 3
Lectures on Numerical Methods
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Topics Designing a Program Input, Processing, and Output
Fundamental Data Types
Summary of what we learned yesterday
Topics Designing a Program Input, Processing, and Output
Module 2 Variables, Data Types and Arithmetic
C++ for Engineers and Scientists Second Edition
Variables and Constants
Presentation transcript:

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

本章重點 Enumerated type Declaring a function parameter Bisection method

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 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 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 p.857, limits.h, float.h %e : print DBL_MIN, DBL_MAX in scientific notation

7.1 (cont) Integer Types in C Range in Typical Microprocessor Implementation short -32767 ~ 32767 unsigned short 0 ~ 65535 int unsigned long -2147483647 ~ 2147483647 unsigned long 0 ~ 4294967295

7.1 (cont) Floating-Point Types in C Approximate Range* Significant Digits* float 10-37 ~1038 6 double 10-307 ~10308 15 long double 10-4931 ~104932 19

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 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 variable initialized int k = 5, m = 4, n; double x = 1.5, y = 2.1, z;

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

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

7.3 Enumerated Types Enumerated type Enumeration constant 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 (cont’d)

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

7.3 (cont) Enumerated Type Definition Syntax: typedef enum {identifier_list} enum_type; Example: {sunday, monday, tuesday, wednesday, thursday, friday, saturday} day_t;

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

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

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

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.9 Three Possibilities That Arise When the Interval [xleft, xright] Is Bisected

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 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 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 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 (cont’d)

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

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