Introduction to Abstract Data Types

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
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)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Data types and variables
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
1 C++ Pointers Gordon College. 2 Regular variables Regular variables declared –Memory allocated for value of specified type –Variable name associated.
Chapter 2 Data Types, Declarations, and Displays
2 Systems Architecture, Fifth Edition Chapter Goals Describe numbering systems and their use in data representation Compare and contrast various data.
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.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
Objectives You should be able to describe: Data Types
Simple Data Type Representation and conversion of numbers
Number Systems Spring Semester 2013Programming and Data Structure1.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Lecture DS & Algorithms:09 Abstract Data Types. Lecture DS & Algorithms:09 2 Abstract Data Types Data Type: A data type is a collection of values and.
First Program.  The type of all data used in a C program must be specified  A data type is a description of the data being represented ▪ That is, a.
CPS120: Introduction to Computer Science
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
1 Intro to Data Structures and ADTs Chapter 2. 2 Goal of Data Structures Organize data Facilitate efficient … –storage –retrieval –manipulation Select.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Copyright © – Curt Hill Types What they do.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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 –
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.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
1 Objects Types, Variables, and Constants Chapter 3.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
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
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.
Programming and Data Structure
The Machine Model Memory
Chapter 2: Introduction to C++
Number Representation
Data Representation Binary Numbers Binary Addition
Chapter 6: Data Types Lectures # 10.
Tokens in C Keywords Identifiers Constants
Documentation Need to have documentation in all programs
EPSII 59:006 Spring 2004.
Understand Computer Storage and Data Types
C Basics.
Data Structures Mohammed Thajeel To the second year students
Invitation to Computer Science, Java Version, Third Edition
Data Structures and Abstract Data Types
Simple Data Types and Function Calls
C++ Data Types Data Type
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Computer Organization
Intro to Data Structures and ADTs
Variables & Basic Types
Data Structures Goal: organize data Criteria: facilitate efficient
Names of variables, functions, classes
Assistant Professor Rabia Koser Dept. of Computer Applications
Variables and Constants
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Introduction to Abstract Data Types Chapter 2 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Contents 2.1 A first look at ADTs and Implementations 2.2 C++'s Simple Data Types 2.3 Programmer-Defined Data Types typedef vs enum 2.4 Pointers Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Objectives Distinguish between ADTs and implementations of ADTs Review C++'s simple data types & ADTs they model See examples of how implementations of ADTs are sometimes insufficient Look at simple mechanisms to define new data types Take a first look at pointers and pointer operations Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

First Look at ADTs & Implementations For a programming task we must identify The collection of data items Basic operations to be performed on them Taken together (data items & operations) are called an Abstract Data Type (ADT) Implementation Storage structures for the data items Algorithms for the operations Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

C++ Simple Data Types Integers Unsigned integers unsigned short, unsigned, unsigned long Sometimes called whole numbers Represented in 2, 4, or 8 bytes Signed integers short, int, long represented in two's complement Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Two's Complement Representation For nonnegative n: Use ordinary base-two representation with leading (sign) bit 0 For n < 0 Find w-bit base-2 representation of |n| Complement each bit. Add 1 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Two's Complement Representation Example: –88 88 as a 16-bit base-two number 0000000001011000 Complement this bit string 1111111110100111 Add 1 1111111110101000 WHY? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Two's Complement Representation Works well for arithmetic computations 5 + –6: 0000000000000101 +1111111111111010 1111111111111111 What gets done to the bits to give this answer? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Problems with Integer Representation Limited Capacity — a finite number of bits An operation can produce a value that requires more bits than maximum number allowed. This is called overflow . None of these is a perfect representation of (mathematical) integers Can only store a finite (sub)range of them. See Demonstrations Fig. 2.1, Fig. 2.2 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

C++ Simple Data Types Real Data Types float and double in C++ Use single precision (IEEE Floating-Point) Store: sign of mantissa in leftmost bit (0 = +, 1 = – ) represent exponent in next 8 bits (exponent + 127) bits b2b3 . . .b24 mantissa in rightmost 23 bits. Need not store b1 — (we know it's 1) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Real Data Example: 22.625 = 10110.1012 Floating point form: 1.01101012 * 24 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Problems with Real Representation Exponent overflow and underflow Round off error Most reals do not have terminating binary representations. Example: 0.7 = (0.10110011001100110011001100. . .)2 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Problems with Real Representation Round off error may be compounded in a sequence of operations. Real-world example – Gulf War Patriot missile guidance affected by accumulated round off Be careful in comparing reals with == and !=. Instead use comparison for closeness if (abs (x – 12.34) < 0.001) … Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

C++ Simple Data Types Character Data 1 byte for ASCII, EBCDIC 2 bytes for Unicode (java) or C++ wide character type Operations ==, <, >, etc. Using numeric code Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

C++ Simple Data Types Boolean Data Values { false, true } Could be stored in bits, usually use a byte Operations &&, || In C++ bool type int (boolVal) evaluates to 0 if false 1 if true Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Programmer-Defined Data Types Typedefs Mechanism usable to create a new type Give new name to existing type Example: typedef double real; Now either double or real can be used. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Programmer-Defined Data Types Enumerations Mechanism for creating types whose literals are identifiers Each identifier associated with unique integer Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Programmer-Defined Data Types Also possible to specify explicit values to give the enumerators enum NumberBase { BINARY = 2, OCTAL = 8, DECIMAL = 10, HEXADECIMAL = 16}; Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Pointers When regular variables are declared Memory allocated for value of specified type Variable name associated with that memory location Memory initialized with values provided (if any) 27 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Pointers Pointer Variables Note sample program, Fig. 2.4 value stored is a memory address Note sample program, Fig. 2.4 Declares variables that can store int addresses and double variables Displays the addresses Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Basic Pointer Operations Dereferencing and indirection Pointer variable stores address of a location Accessing contents of that location requires dereferencing operator * Note program example Fig 2.5 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Basic Pointer Operations Assignment Pointer variables can be assigned the values of other pointer variables bound to same type Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Basic Pointer Operations Consider *jPtr = 44; Changes value that both pointers reference Not good programming practice, hard to debug Known as aliasing problem Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Basic Pointer Operations Comparison Relational operators used to compare two pointers Must be bound to same type Most common = = and != The null address may be compared with any pointer variable Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Dynamic Memory Allocation The new operation Example int * intPtr; intPtr = new int; An anonymous variable Cannot be accessed directly Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Pointer Arguments Pointers can be passed as arguments to functions This is logically equivalent to reference parameters In fact, this is how early C++ compilers accomplished reference parameters Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3