Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.

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 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Common Programming Errors Syntax Errors (Figure 2.15) –missing semicolon at the end of the variable.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 11 Structure and Union 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.
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
Instructor: Sanchita Mal-Sarkar Course: CIS 265
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.
Objectives You should be able to describe: Data Types
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Simple Data Type Representation and conversion of numbers
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Computer Science 111 Fundamentals of Programming I Number Systems.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
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.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Simple Data Types Problem Solving, Abstraction, and Design using.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Simple Data Types Built-In and User Defined Chapter 10.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
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.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
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
Chapter 4 C Program Control Part I
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++.
The Selection Structure
Basic Elements of C++ Chapter 2.
Introduction to Abstract Data Types
Simple Data Types and Function Calls
Lectures on Numerical Methods
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Presentation transcript:

Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-2 本章重點  Enumerated type  Declaring a function parameter  Bisection method

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-3 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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-5 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 2 exponent

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-6 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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Integer Types in C TypeRange in Typical Microprocessor Implementation short ~ unsigned short0 ~ int ~ unsigned0 ~ long ~ unsigned long0 ~

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Floating-Point Types in C TypeApproximate Range* Significant Digits* float ~ double ~ long double ~

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Automatic Conversion of Data Types  variable initialized  int k = 5, m = 4, n;  double x = 1.5, y = 2.1, z;

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Automatic Conversion of Data Types Context of ConversionExampleExplanation 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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 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’) …

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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’

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-17 Figure 7.3 Program to Print Part of the Collating Sequence

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 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.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-19 Figure 7.4 Enumerated Type for Budget Expenses

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-20 Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-21 Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Enumerated Type Definition  Syntax : typedef enum {identifier_list} enum_type;  Example : typedef enum {sunday, monday, tuesday, wednesday, thursday, friday, saturday} day_t;

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-24 Figure 7.5 Accumulating Weekday Hours Worked

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 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.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-26 Figure 7.6 Six Roots for the Equation f(x) = 0

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-27 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.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Calls to Function evaluate and the Output Produced Call to evaluateOutput Produced evaluate(sqrt, 0.25, 25.0, 100.0) f( )= f( )= f( )= evaluate(sin, 0.0, , 0.5* ) f( )= f( )= f( )=

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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 x mid = x left + x right 2.0

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-31 Figure 7.8 Change of Sign Implies an Odd Number of Roots

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-32 Figure 7.9 Three Possibilities That Arise When the Interval [xleft, xright] Is Bisected

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (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)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-36 Figure 7.10 Finding a Function Root Using the Bisection Method

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-37 Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-38 Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-39 Figure 7.11 Sample Run of Bisection Program with Trace Code Included

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 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

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-41 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.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-42 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.