Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.

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.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
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
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Instructor: Sanchita Mal-Sarkar Course: CIS 265
Chapter 2 Data Types, Declarations, and Displays
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
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.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 2 Bits, Data Types & Operations Integer Representation Floating-point Representation.
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.
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Computer Arithmetic Nizamettin AYDIN
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Number Systems Spring Semester 2013Programming and Data Structure1.
C Tokens Identifiers Keywords Constants Operators Special symbols.
chap7 Chapter 7 Simple Data Types chap7 2 Objectives No programming language can predefine all the data types that a programmer may.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Round-off Errors and Computer Arithmetic. The arithmetic performed by a calculator or computer is different from the arithmetic in algebra and calculus.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
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.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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 –
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Programming and Data Structure
Number Representation
Tokens in C Keywords Identifiers Constants
Fundamental Data Types
Chapter 2 Bits, Data Types & Operations Integer Representation
Conversions of the type of the value of an expression
Introduction to Abstract Data Types
INPUT & OUTPUT scanf & printf.
Simple Data Types and Function Calls
Lectures on Numerical Methods
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Fundamental Data Types
Presentation transcript:

Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-2 Representation and Conversion of Numeric Types Simple data type –a data type used to store a single value Differences between numeric types –integers are faster –storage space –loss of accuracy with type double numbers –data represented in memory is computer dependent

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-3 Floating-point Format double divided into two sections: the mantissa and the exponent mantissa –between 0.5 and 1.0 for positive numbers and between -0.5 and -1.0 for negative –real number = mantissa X 2 exponent

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-4 Figure 7.1 Internal Formats of Type int and Type double

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-5 Floating-point Format (Cont’d) Actual ranges vary from one implementation to another The ANSI standard for C specifies the minimum range of positive values of type int is from 1 to 32,767. The minimum range specified for positive values of type double is from to

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-6 Figure 7.2 Program to Print Implementation- Specific Ranges for Positive Numeric Data

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-7 Floating-point Format (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-8 Floating-point Format (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-9 Numerical Inaccuracies Some fractions cannot be represented exactly as binary numbers in the mantissa Representational error –an error due to coding a real number as a finite number of binary digits The representational error (sometimes called roundoff error) will depend on the number of binary digits (bits) used in the mantissa.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-10 Numerical Inaccuracies (Cont’d) The result of adding 0.1 one hundred times may not be exactly 10.0, so the following loop may fail to terminate. for (trial = 0.0; trial != 10.0; trial = trial + 0.1) {... } If the loop repetition test is changed to trial < 10.0, the loop may execute 100 times on one computer and 101 times on another. It is best to use integer variables for loop control

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-11 Numerical Inaccuracies (Cont’d) When you add a large real number and a small number, the larger number may “cancel out” the smaller number. cancellation error –an error resulting from applying an arithmetic operation to operands of vastly different magnitudes; effect of smaller operand is lost 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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-12 Automatic Conversion of Data Types int k = 5, m = 4, n; double x = 1.5, y = 2.1, z;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-13 Explicit Conversion of Data Types cast –an explicit type conversion operation Assume int nt, d1; Compare –frac = (double)n1 / (double)d1; –frac = n1 / d1; converting only one would be sufficient, because the rules for evaluation of mixed-type expressions would then cause the other to be converted as well. –Note: assignment is not included in this rule But, frac = (double)(n1 / d1); resulting in the loss of the fractional part!! Why?

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-14 Explicit Conversion of Data Types (Cont’d) We sometimes include casts that do not affect the result but simply make clear to the reader the conversions that would occur automatically. Equivalent (int sqrt_m, m;) –sqrt_m = sqrt(m); –sqrt_m = (int)sqrt((double)m);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-15 Representation and Conversion of Type char Character values may also be compared, scanned, printed, and converted to type int. Each character has its own unique numeric code; the binary form of this code is stored in a memory cell. Character Codes in Appendix A: ASCII, EBCDIC, CDC

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-16 Representation and Conversion of Type char (Cont’d) Order relationship –'0' < '1' < '2' < '3' < '4' < '5' < '6' < '7' < '8' < '9' –'A' < 'B' < 'C' <... < 'X' < 'Y' < 'Z' –'a' < 'b' < 'c' <... < 'x' < 'y' < 'z' not necessarily consecutive.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-17 Representation and Conversion of Type char (Cont’d) In ASCII, the printable characters have codes from 32 (code for a blank or space) to 126 (code for the symbol ~). The other codes represent nonprintable control characters. –Sending a control character to an output device causes the device to perform a special operation such as returning the cursor to column one. C permits conversion of type char to type int and vice versa. collating sequence –a sequence of characters arranged by character code number

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-18 Figure 7.3 Program to Print Part of the Collating Sequence

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-19 Enumerated Types Enumerated type –a data type whose list of values is specified by the programmer in a type declaration Example typedef enum {entertainment, rent, utilities, food, clothing, automobile, insurance, miscellaneous} expense_t; expense_t expense_kind;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-20 Enumerated Types (Cont’d) Enumeration constant –an identifier that is one of the values of an enumerated type expense_t causes the enumeration constant entertainment to be represented as the integer 0, rent to 1, utilities to 2, and so on.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-21 Figure 7.4 Enumerated Type for Budget Expenses

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-22 Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-23 Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-24 Enumerated Types (Cont’d) Enumeration constants must be identifiers; they cannot be numeric, character, or string literals. An identifier cannot appear in more than one enumerated type definition. Relational, assignment, and even arithmetic operators can be used with enumerated types.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-25 Enumerated Types (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-26 Enumerated Types (Cont’d) Example –sunday < monday –wednesday != friday –tuesday >= Sunday Example day_t tomorrow; if (today == saturday) tomorrow = sunday; else tomorrow = (day_t)(today + 1);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-27 Enumerated Types (Cont’d) C handles enumerated type values just as it handles other integers, no range checking to verify that the value stored in an enumerated type variable is valid. –today = saturday + 3; is invalid

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-28 Figure 7.5 Accumulating Weekday Hours Worked

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-29 Figure 7.6 Six Roots for the Equation f(x) = 0

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-30 Figure 7.7 Using a Function Parameter

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-31 Figure 7.8 Change of Sign Implies an Odd Number of Roots

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-32 Figure 7.9 Three Possibilities That Arise When the Interval [xleft, xright] Is Bisected

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-33 Figure 7.10 Finding a Function Root Using the Bisection Method

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-34 Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-35 Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-36 Figure 7.11 Sample Run of Bisection Program with Trace Code Included

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-37 Figure 7.12 Geometric Interpretation of Newton's Method

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-38 Figure 7.13 Approximating the Area Under a Curve with Trapezoids

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-39 Figure 7.14 Finite State Machine for Numbers and Identifiers