Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Similar presentations


Presentation on theme: "Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008."— Presentation transcript:

1 Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008

2 Outline Introduction to Abstract Data Types (ADTs) Arrays

3 A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order to do that, we need to identify: 1. The collection of data items 2. Basic operation that must be performed on them Abstract Data Type (ADT): a collection of data items together with the operations on the data

4 Abstract Data Type (ADT) The word “abstract” refers to the fact that the data and the basic operations defined on it are being studied independently of how they are implemented We think about what can be done with the data, not how it is done

5 ADT example

6 Implementation of ADT An implementation of ADT consists of storage structures to store the data items and algorithms for basic operation

7 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 How to store, access data?

8 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 How to store, access data? 10 individual variables

9 Use 10 individual variables Algorithm to List available seats 1. If seat1 == ‘ ’: display 1 2. If seat2 == ‘ ’: display 2. 10. If seat10 == ‘ ’: display 10 Algorithm to Reserve a seat 1. Set DONE to false 2. If seat1 ==‘ ’: print “do you want seat #1??” Get answer if answer==‘Y’: set seat1 to ‘X’ set Done to True 3. If seat2 ==‘ ’: print “do you want seat #2??” Get answer if answer==‘Y’: set seat2 to ‘X’ set Done to True.

10 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 How to store, access data? 10 individual variables An array of variables

11 Use Array Algorithm to List available seats For number ranging from 0 to max_seats-1, do: If seat[number] == ‘ ’: Display number Algorithm to Reserve a seat Readin number of seat to be reserved If seat[number] is equal to ‘ ’: set seat[number] to ‘X’ Else Display a message that the seat having this number is occupied

12 ADTs In this simple example, it does illustrate the concept of an Abstract Data Type ADT consists of 1. The collection of data items 2. Basic operation that must be performed on them In the example, a collection of data is a list of seats The basic operations are (1) Scan the list to determine which seats are occupied (2) change seat’s status

13 Data Structure and Abstract Data Type The term of Data Structure and Abstract Data Type are often used interchangeably However, we use ADT when data is studied at a logical level The term data structure refers to a construct in programming language that can be used to store data

14 Outline Introduction to Abstract Data Types (ADTs) Arrays

15 Array ADT Collection of data elements A fixed-size sequence of elements, all of the same type Basic Operations Direct access to each element in the array by specifying its position so that values can be retrieved from or stored in that position

16 Different types of Array One-dimensional array: only one index is used Multi-dimensional array: array involving more than one index Static array: the compiler determines how memory will be allocated for the array Dynamic array: memory allocation takes place during execution

17 One-Dimensional Static Array Syntax: ElementType arrayName [CAPACITY]; ElementType arrayName [CAPACITY] = { initializer_list }; Example: int b [10]; int b [10]={1,2,3,4,5,6,7,8,9,10};

18 Array and ADT Collection of data elements A fixed-size sequence of elements, all of the same type Basic Operations Direct access to each element in the array by specifying its position so that values can be retrieved from or stored in that position An Array as an ADT C++ Array Fixed size ----------------------specify the capacity of the array Ordered-------------------------indices are numbered 0,1,2,…,capacity-1 Same type of elements-------specify the element type Direct access-------------------subscript operator[]

19 Example of array Consider array a,b,c,d to store collection of 10 integers declared by: int capacity=10 int a[capacity], b[capacity]={1,2,3,4,5,6,7,8,9,10}, c[capacity]={1,2,3}, d[capacity]={0}; char name[capacity]=“John Doe”;

20 Array Output Function Void display(int array[], int num_values) { for (int I = 0; i<num_values; i++) cout<< array[i] << “ ”; }

21 Multidimensional Arrays Consider a table of test scores for several different students

22 Array of Array Declarations An array of arrays An array whose elements are other arrays

23 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]

24 Multidimensional Arrays Syntax: ElementType arrayName [num_rows][num_columns]; int scoretable [num_students][num_tests]; int scoretable[2][5]={{80,80,80,80,80},{60,60,60,60,60}}; If you want to change the score of first student’s 3 rd test score to 100, you just need to do: Scoretable[0][2]=100

25 Multidimensional Arrays Consider multiple pages of the student grade book typedef double ThreeDimArray[NUM_ROWS][NUM_COLS][NUM_RANKS];...


Download ppt "Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008."

Similar presentations


Ads by Google