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

Slides:



Advertisements
Similar presentations
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Advertisements

Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
ISBN Chapter 6 Data Types: Structured types.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Microsoft Visual C++.NET Chapter 61 Memory Management.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
1 Intro to Data Structures and ADTs Chapter 2. 2 Goal of Data Structures Organize data Facilitate efficient … –storage –retrieval –manipulation Select.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Data Types In Text: Chapter 6.
More about OOP and ADTs Classes
Data Structures and Abstract Data Types
Array Lists Chapter 6 Section 6.1 to 6.3
Introduction to Abstract Data Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
More about OOP and ADTs Classes
Objectives You should be able to describe: Addresses and Pointers
Abstract Data Types, Elementary Data Structures and Arrays
Intro to Data Structures and ADTs
Data Structures Goal: organize data Criteria: facilitate efficient
Presentation transcript:

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Data Structures and Abstract Data Types Chapter 3

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Contents 3.1 Data Structures, Abstract Data Types and Implementations 3.2 Static Arrays 3.3 Multidimensional Arrays (optional) 3.4 Dynamic Arrays 3.5 C-Style Structs (optional) 3.6 Procedural Programming

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Objectives Look at ADTs, implementations in detail Introduce arrays as ADTs See arrays implemented as C++ static arrays (Optional) Describe multidimensional arrays Extend pointers to use in dynamic arrays (Optional) Show use of C++ structs to model objects with multiple attributes Show example of procedural programming paradigm

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Data Structures, Abstract Data Types, and Implementations Consider example of an airplane flight with 10 seats to be assigned Tasks –List available seats –Reserve a seat –Cancel a seat How to store, access data? –10 individual variables –An array of variables

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Data Structures, Abstract Data Types, and Implementations Implementation consists of –Storage (data) structures –Algorithms for basic operations Note following figure –C++ provides large collection of data types and structures

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved C++ Types

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Arrays Collection of data elements –All of same type –Each accessed by specifying position Static array –Compiler determines how memory allocated Dynamic array –Allocation takes place at run time

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Single Dimension Arrays Syntax: ElementType arrayName [CAPACITY]; ElementType arrayName [CAPACITY] = { initializer_list }; Example: int b [10]; Elements accessed by –name and [ ] operation b[5]

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Character Arrays Elements of an array may be of any type –Including characters Example: char name [NAME_CAPACITY] = "John Doe"; If array initialized shorter than specs –extra locations filled with null character

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Subscript Operation We have said elements accessed by name and [ ] numList[5] Consider the [ ] to be an operator –The subscript operator –Performs address translation Name of the array is a pointer constant –The base address

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Using Arrays Accessing array for output –See Fig. 3.3Fig. 3.3 Accessing array for input from keyboard –See Fig. 3.4Fig. 3.4 Note use of arrays as parameters –Must specify number of elements of array being used

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Out of Range Errors Most C++ compilers do not by default check indices for out of range Results of out of range array access –Program can exceed allowed memory area –Program can give puzzling results Note example, Fig. 3.5Fig. 3.5

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Problems with C-Style Arrays Capacity cannot change. An array is not an object –(in the OOP sense) Virtually no predefined operations for non-char arrays. The Deeper Problem: – C-style arrays aren't self-contained – Data, functions, and size not encapsulated

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Multidimensional Arrays Consider a table of test scores for several different students Two-dimensional array –Syntax ElementType arrayName [numRows][numCols]

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Multidimensional Arrays Consider multiple pages of the student grade book const int NUM_ROWS = 10, NUM_COLS = 5, NUM_RANKS = 10; typedef double ThreeDimArray[NUM_ROWS][NUM_COLS][NUM_RANKS];...

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Array of Array Declarations An array of arrays –An array whose elements are other arrays

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Array of Array Declarations Each of the rows is itself a one dimensional array of values scoresTable[2] is the whole row numbered 2 scoresTable[2] is the whole row numbered 2 scoresTable [1][3]

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Memory Allocation in 2-Dimensional Arrays Elements stored in rowwise order Also called column major order location [0][4] is followed in memory by location [1][0]

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Multidimensional Arrays as Parameters Must specify –The type and name of the array (a pointer to the base address) –The number of rows –The number of columns (or the length of each row) Note example programexample program

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Dynamic Arrays Recall earlier mention of arrays being fixed size at compile time –Space wasted by unused elements –Program cannot adjust if size set too small Dynamic (run time) allocation mechanism provided –Acquire memory as needed –Release memory when no longer needed C++ commands –new and delete

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved The new Operator Syntax for arrays new Type [capacity] This command issues a run-time request for a block of memory –Asks for enough memory for the specified number of elements of the stated type Example int *arrayPtr; arrayPtr = new int[6];

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Pointer Arithmetic Possible to alter pointer contents –The pointer is a variable –It is not a pointer constant like an array name Example Given: Then ptr++;

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Failures of new When new execute –Requests memory from heap or free store –Allocates that block to executing program Possible to use up available heap memory –If not enough memory for request, new throws an exception … bad_alloc Note sample program, Fig. 3.8Fig. 3.8 Possible to use try and catch mechanism to take appropriate action, Fig 3.9Fig 3.9

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved The delete Operation Counterpart to the new operation Requests memory be returned to the heap –Can then be reused by later allocations Syntax delete pointerVariable; delete [ ] arrayPointerVariable; Frees the dynamically memory whose address is stored in the variable –Does not delete the variable

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Memory Leaks Important for programmer to make sure to deallocate memory originally allocated by new What if new is called again for intPtr ? Originally allocated memory now cannot be accessed, nor is it available for reallocation

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Other Uses of Pointers Command-line arguments –argc is the counter – how many arguments are found –argv is a pointer to the array of pointers to char (the arguments) Functions as arguments –The name of a function is a pointer to code –Can be passed to another function

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Aggregate Data Types Predefined types not always adequate to model the problem –When objects have multiple attributes –When objects have collections of heterogeneous elements C++ provides structs and classes –Create new types with multiple attributes

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Structures Characteristics –has a fixed size –is ordered –elements may be of different size –direct access of elements by name (not index) struct Date { int month, day, year; char dayOfWeek [12]; };

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved FAQs about Structures structs can be nested (can contain struct objects) Access members with –name of struct object –dot (member selector operator). –name of struct member Date today = { 3, 4, 2005, "Tuesday"); cout << today.month;

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Pointers to Structs Pointers can be bound to any type Date today = { 3, 4, 2005, "Tuesday"); Date *datePtr = &today; Use for accessing the original location cout > datePtr->year; Note use of arrow -> operator

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Description of Procedural Programming Typical languages : C, FORTRAN, and Pascal Action-oriented — concentrates on the verbs Programmers: –Identify basic tasks to solve problem –Implement actions to do tasks as subprograms (procedures/functions/ subroutines) –Group subprograms into programs/modules/libraries, –together make up a complete system for solving the problem

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Example of Procedural Programming Consider a type for processing times –In hh:mm AM/PM format –In military format (24 hr clock) We must determine –Data attributes –Operations on the data –How to implement Note sample header, Fig. 3-11AFig. 3-11A Implementation, Fig. 3-11BFig. 3-11B Test program, Fig. 3-11CFig. 3-11C