Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 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)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
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.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Input & Output: Console
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
1 Objects Types, Variables, and Constants Chapter 3.
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 6: Data Types Lectures # 10.
Multiple variables can be created in one declaration
(2-1) Data Structures & The Basics of a Linked List I
Chapter 2.
(2-1) Data Structures & The Basics of a Linked List I
Introduction to Abstract Data Types
Simple Data Types and Function Calls
C Structures, Unions, Bit Manipulations and Enumerations
Presentation transcript:

Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University

2 Data Types Data type = set of values + set of operations on those values Ex: int data type in C ( Microsoft Visual C represents this type by 32 bits) int Set of values: “integers” in range [-2,147,483,648, +2,147,483,647] Operations: arithmetic operators: +, -, *, /, % (mod) relational operators: >, = equality operators: ==, != …

Internal Representation of int and double (1) We learned that all values stored in a computer are represented as sequences of 0’s & 1’s. Other than the sequential structure of memory, for many applications, programmer doesn’t know/care about the underlying representations of the data types. As you already learned int and double have different internal formats: real number = mantissa * 2 exponent Binary fraction between [0.5,1] or [-1,-0.5] integer

If we’re dealing with very big or very small numbers, we may want to check into how many bits are used to represent int, double C supports a variety of different integer and double formats: Internal Representation of int and double (2) Type# bits in Microsoft Visual C short16 unsigned short 16 int32 unsigned int32 long32 unsigned long 32 Type# bits in Microsoft Visual C float32 double64 long double64

5 Representation/ round-off errors ◦ Just as certain fractions cannot be represented exactly in the decimal number system (e.g the fraction 1/3 is …), some fractions cannot be represented as binary numbers in the mantissa of the type double format. Because of this:  Don't rely on equality operators while dealing with floating-point values: for (trial = 0; trial != 10.0; trial += 0.1) { … }  Don't rely on comparison operators while dealing with floating-point values, either. Following may not execute the same number of times on all computers: for (trial = 0; trial < 10.0; trial += 0.1) { … }  Therefore, it's better to use integers as loop counters! Internal Representation of int and double (3)

Conversions between int and double  When we assign an int to a double or a double to an int, C performs an automatic conversion: int k = 5, m = 4, n; double x = 2.5, y = 4.1, z; z = k + x; //k is converted to double prior to + z = k / m; //k/m is evaluated first //result 1 is then converted to double 1.0 n = x * y; //x*y is evaluated first to //Result is then converted to int 10 //Fractional part is lost during this conversion Internal Representation of int and double (4) C. Hundhausen, A. O’Fallon

Conversions between int and double (cont.)  Note that explicit casting is always an option: int k = 5, m = 4, n; double x = 2.5, y = 4.1, z; z = (double) k + x; // 7.5 z = (double) k / (double) m; //1.25 n = (int) x * (int) y; // 8  But such casts happen on the fly and so do not change the internal representation of a variable: printf(“%.2f\n", (double) k); // 5.00 printf("%4d\n",k); // 5 /* After these statements are executed, k is still stored as the int 5 */ Internal Representation of int and double (5) C. Hundhausen, A. O’Fallon

8 Internal Representation of char (1) As we have learned, char variables are stored in 8 bit ASCII (American Standard Code for Information Interchange) format ◦ '0'.. '9': 48 – 57 ◦ 'A'.. 'Z': 65 – 90 ◦ 'a'.. 'z': 97 – 122 ◦ Printable characters: 32 – 122 ◦ Non-printable control characters: 0 – 31 and 127 (sending a control character to an output device causes the device to perform a special operation such as advancing the cursor to the next line) ◦ See Appendix A for the details

9 Internal Representation of char (2) It is possible to cast between char and int : int char_code; for (char_code = (int) 'A'; char_code <= (int) 'Z'; ++char_code) { printf("%c", (char) char_code); } Yields the following: ABCDEFGHIJKLMNOPQRSTUVWXYZ Casting is sometimes done to increase readability of the code (even if it doesn’t effect the result). In the case of conversion between char to int, as you’ve already seen, we don’t need explicit casting. Above code would work just fine without the explicit casts.

Enumerated Types (1) Often, we may need to define our own custom data types, specifying the possible values of it. Examples: ◦ days of the week ◦ months of the year ◦ household budget categories ◦ business inventory categories ◦ etc. C enumerated types allow us to do this. For example enumerated type inventory_t has 6 possible values typedef enum { clothing, household, electronics, garden, health_beauty, sporting_goods } inventory_t; Note 1: clothing gets integer value 0, household gets integer value 1,... sporting_goods gets integer value 5. Note 2: Remember, we don’t really care how they are represented by C, just like in the case of char data type. All we need to know is that each will get unique values and these values will be ordered in the order we’ve listed them.

11 Enumerated Types (2) Once we've defined an enumerated type, we can declare variables of that type: inventory_t inv_kind; We can initialize these variables just like we do in any data type: inventory_t this_inventory = household; // Recall a similar situation with an char type: char x = ‘a’; We can make direct comparisons: if (household < sporting_goods) // true if (electronics != health_beauty) // true // Recall a similar situation with an char type: if(‘a’ < ‘t’) inventory_t invent2 = household; inventory_t invent1 = sporting_goods; if (invent2 < invent1 ) // true ;

C. Hundhausen, A. O’Fallon12 Enumerated Types (3) We can use the type in switch statements: void print_inventory(inventory_t inv_kind) { switch (inv_kind) { case clothing: printf("clothing"); break; case household: printf("household"); break; case electronics: printf("electronics"); break; case garden: printf("garden"); break; case health_beauty: printf("health and beauty"); break; case sporting_goods: printf("sporting goods"); break; }

C. Hundhausen, A. O’Fallon, G.Cilingir13 Enumerated Types (4) We can "scroll" through items: inventory_t this_inventory = household; while (this_inventory <= sporting_goods) { print_inventory(this_inventory); this_inventory++; } Recall a similar situation with an int type (to understand what’s happening here) char ch = ‘a’; while (ch <= ‘z’) { print_char(counter); ch++; }

C. Hundhausen, A. O’Fallon14 Enumerated Types (5) We can even cast enumerated types to int : int household_val; inventory_t this_inventory; household_val = (int) household // 0..and cast integers to the enumerated type: this_inventory = (inventory_t)(electronics + 1); // garden

C. Hundhausen, A. O’Fallon15 Other Common Enumerated Types? typedef enum { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } Month; typedef enum { FALSE, TRUE } Boolean;

16 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.