CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
Programming and Data Structure
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
ISBN Chapter 6 Data Types: Structured types.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
Arrays. The array data structure An array is an indexed sequence of components –Typically, the array occupies sequential storage locations –The length.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Dynamic Structures & Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
C++ Review (3) Structs, Classes, Data Abstraction.
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.
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.
Arrays & Structures II Chapter 9. 2 Organizing Heterogeneous Data  Arrays allow a programmer to organize values of the same type  Homogeneous data 
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Variables and memory addresses
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
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.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Student Book An Introduction
Chapter 16 Linked Structures
Arrays.
Managing 2D Arrays Simple Dynamic Allocation
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Presentation transcript:

CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item int holds one integer value char holds one character value Structured – represents a collection of items and has: domain - component data items each of which may be simple or another structured data type structure - rules defining the relations between the components operations - a set of operations or behaviors

CPSC 252 Concrete Data Types Page 2 A taxonomy of data structures

CPSC 252 Concrete Data Types Page 3 Linear vs. non-linear Linear: There is a unique first and unique last element and every other element has a unique predecessor and a unique successor, except for the first and last an array is a linear data structure there is a first (0th) element and a last element every other element has an k-1st and a k+1st element Non-linear: Any data structure that is not linear a set is a non-linear data structure there is no unique first or last element there is no obvious ordering of the elements

CPSC 252 Concrete Data Types Page 4 Direct-access vs. sequential access Linear data structures can be further classified as: Direct access: elements can be accessed at random, in any order, for example: an array is direct access any element can be accessed at any time Sequential access: elements must be accessed in some specified order, for example: a queue is sequential access element can only be accessed one after the other

CPSC 252 Concrete Data Types Page 5 Homogeneous vs. heterogeneous Some data structures can further be classified as: Homogeneous: all the elements in the structure are of the same data type an array Heterogeneous (non-homogeneous): the elements in the structure are of different data types a record or struct in C++

CPSC 252 Concrete Data Types Page 6 Structs vs. classes in C++ A struct is just like a class with the exception that, by default, the members are public not private. struct date { int day; string month; int year; }; We tend to use a struct for concrete data types (CDT) a class for abstract data types (ADT) Both can have methods and public, private (and protected) members

CPSC 252 Concrete Data Types Page 7 Concrete Data Types vs. Abstract Data Types Concrete Data Types (CDTs) are direct implementations of fairly simple structures. Abstraction and information hiding are not employed when designing CDTs. Abstract Data Types (ADTs) offer a high-level, easy-to-use interface that is independent of the ADT’s implementation. Example: We can implement a student record either as a CDT or an ADT: CDT: implement it as a struct where all the data is public: struct student { string name; int idNum; };

CPSC 252 Concrete Data Types Page 8 Example (cont’d): We can implement a student record either as a CDT or an ADT: ADT: implement it as a class where the data is private but a set of public operations is defined that allow the client to operate on the data. class student { private: string name; int idNum; public: string getName() const; void setName( string ); int getIdNum() const; void setIdNum( int ); };

CPSC 252 Concrete Data Types Page 9 Records A record is linear, direct access, heterogeneous, concrete data structure that groups data of potentially differing data types into a single unit in C++ records are implemented using a struct Domain: a collection of fields (items) of potentially different data types and a set of names for the fields in each record Structure: objects are stored in consecutive memory locations and each object has a unique name Operations: the “.” operator provides access to each field in a record

CPSC 252 Concrete Data Types Page 10 Example: struct Course { string dept; int number; }; Course compCourse; compCourse.dept = “CPSC”; compCourse.number = 252; Note that we have direct access to the individual data members using the “.” operator No attempt is made to “hide” the data behind a set of public operations

CPSC 252 Concrete Data Types Page 11 Arrays An array is a linear, direct access, homogeneous, concrete data structure Domain: a collection of n items of the same data type and a set of indexes in the range [ 0, n-1 ] Structure: items are stored in consecutive locations each having a unique index Operations: the indexing operator “[]” gives read and write access to the item at the specified index

CPSC 252 Concrete Data Types Page 12 Arrays and pointers in C++ There is a strong connection between arrays and pointers The declaration int data1[ 3 ]; allocates memory for three integers associates the name data1 with the address of the 1st byte of this memory data1 can be thought of as a constant pointer The declaration int* data2 = new int[ 3 ]; allocates memory for 1 pointer and also 3 integers data2 is a pointer that is not constant data1 data2

CPSC 252 Concrete Data Types Page 13 Pointer Arithmetic The operators “+”, “–”, “++” and “––” are overloaded to operate on pointers: data + 2 is a pointer to the 3rd integer in the array data Thus *(data + 2) = 7; and data[ 2 ] = 7; mean exactly the same thing and they can be used interchangeably

CPSC 252 Concrete Data Types Page 14 Notice that there is something rather subtle going on here. By adding the integer 2 to data, we do not produce an address that is 2 bytes higher than the one stored in data. We in fact produce an address that is 8 bytes higher (assuming that each integer occupies 4 bytes): 4 bytes data data + 1 data + 2 So, when we increment or decrement a pointer, the address is changed by an amount equal to the size of the object pointed to! This is very useful, it means that as programmers we do not have to concern ourselves with the size of an object when performing pointer arithmetic. Thus: &data[i] is equivalent to ( data + i )

CPSC 252 Concrete Data Types Page 15 More about arrays and pointers in C++ The following code again illustrates the connection between indexing into an array and pointer arithmetic: int data[ 4 ] = { 4, 3, 2, 1 }; for( int index = 0; index < 4; index++ ) cout << *( data + index ) << “ ”; Arrays can be passed as parameters to functions The function printArray() can be declared to accept an array of integers as a parameter in either of the following ways: void printArray( int data[], int size ); or as void printArray( int* data, int size );

CPSC 252 Concrete Data Types Page 16 Regardless of the way in which the function is declared, it can be implemented in either of the following ways: { for( int index = 0; index < size; index++ ) cout << index << “ ” << data[index] << endl; } or { for( int index = 0; index < size; index++ ) cout << index << “ ” << *(data + index) << endl; }

CPSC 252 Concrete Data Types Page 17 Multidimensional arrays as arrays Suppose we want to store the average rainfall during each hour for a period of one week. We can do so conveniently using a two-dimensional array: float rainfall[ 7 ][ 24 ]; We can think of the two-dimensional array as a table having 7 rows (one for each day) and 24 columns (one for each hour). To set the rainfall on the first day for the hour between noon and 1pm: rainfall[ 0 ][ 12 ] = 1.5; What if we want to use dynamic memory allocation?

CPSC 252 Concrete Data Types Page 18 Multidimensional arrays as pointers We can declare an array of pointers to each row in the table and dynamically allocate memory to each row: int* data[ 3 ]; data[ 0 ] = new int[ 3 ]; data[ 1 ] = new int[ 2 ]; data[ 2 ] = new int[ 4 ]; We can index into the array just as before: data[ 2 ][ 1 ] = 4; Notice that this has given us the flexibility of having a two dimensional array where the rows are of different lengths! We are still have the restriction that if we change the number of rows, we have to re-compile because the dimension of the array of pointers must be a constant

CPSC 252 Concrete Data Types Page 19 Multidimensional arrays as pointers to pointers We can gain the most flexibility by declaring data to be a pointer to a pointer to an integer We can then declare the array of pointers dynamically as well as dynamically allocating memory to each row: int** data; data = new int*[ 2 ]; data[ 0 ] = new int[ 4 ]; data[ 1 ] = new int[ 3 ]; Again, we can index into the array just as before: data[ 1 ][ 2 ] = 5;

CPSC 252 Concrete Data Types Page 20 Tradeoffs between arrays and pointers Advantages of arrays simple structures, no dynamic memory Disadvantages of arrays size is fixed Advantages of pointers size of memory pointed to can vary Disadvantages of pointers must explicitly invoke new and delete