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

Slides:



Advertisements
Similar presentations
Introduction to Data Structures CMPE231 Spring 2012 Assoc. Prof. Alexander Chefranov 1.
Advertisements

Types and Arithmetic Operators
2-1 Chapter 2 - Data Representation Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer Architecture.
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.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 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
Data Representation Kieran Mathieson. Outline Digital constraints Data types Integer Real Character Boolean Memory address.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
2 Systems Architecture, Fifth Edition Chapter Goals Describe numbering systems and their use in data representation Compare and contrast various data.
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
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Computer Science 111 Fundamentals of Programming I Number Systems.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
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.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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 –
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
1.2 Primitive Data Types and 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.
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. 
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 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
Number Representation
Chapter 6: Data Types Lectures # 10.
Documentation Need to have documentation in all programs
EPSII 59:006 Spring 2004.
Data Structures Mohammed Thajeel To the second year students
Invitation to Computer Science, Java Version, Third Edition
Introduction to Abstract Data Types
Simple Data Types and Function Calls
Computer Organization
Intro to Data Structures and ADTs
Variables and Constants
Presentation transcript:

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 Chapter Contents 2.1 A first look at ADTs and Implementations 2.2 C++'s Simple Data Types 2.3 Programmer-Defined Data Types 2.4 Pointers

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

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

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Two's Complement Representation Works well for arithmetic computations 5 + –6: 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 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.2Fig. 2.1Fig. 2.2

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Output of Programs of Fig. 2.1 & 2.2 (GNU C++)./fig /fig

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 b 2 b 3...b 24 mantissa in rightmost 23 bits.  Need not store b 1 — (we know it's 1)

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Real Data Example: = Floating point form: * 2 4

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

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 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 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 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 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 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 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 Pointers Pointer Variables –value stored is a memory address Note sample program, Fig. 2.4Fig. 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 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 Fig 2.5

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 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 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 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 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