Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore www.desiamore.com/ifm DeSiaMorewww.desiamore.com/ifm1.

Similar presentations


Presentation on theme: "Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore www.desiamore.com/ifm DeSiaMorewww.desiamore.com/ifm1."— Presentation transcript:

1 Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore www.desiamore.com/ifm DeSiaMorewww.desiamore.com/ifm1

2 Function for Deleting Array void array::del (int pos) { //skip to the desired position for (int i=pos; i<MAX; i++) arr[i-1] = arr[i]; arr[i-1]=0; } DeSiaMore www.desiamore.com/ifm 2

3 Displaying the array void array::display () { cout<<endl; //Traverse the entire array for (int i = 0; I < MAX; i++) cout<<“ “<<arr[i]; } DeSiaMore www.desiamore.com/ifm 3

4 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 DeSiaMore www.desiamore.com/ifm 4

5 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 DeSiaMore www.desiamore.com/ifm 5

6 DeSiaMore www.desiamore.com/ifm 6

7 Types A type (data type) is a set of values that an object can have. An abstract data type (ADT) is a:  data type  Set of functions (operations) that operates on data of that type  Each function is defined by its signature.  Can also specify operations formally (see section 4.2.5) (Algebraic data types): f(g(A,S)) = A DeSiaMore www.desiamore.com/ifm 7

8 Example ADT ADT student: Operations: SetName: name x student  student GetName: student  name SetCourse: course x student  student GetCourse: student  course * GetCredits: student  integer In this example, Name and Course are undefined ADTs. They are defined by some other abstraction. DeSiaMore www.desiamore.com/ifm 8

9 Real world ADT example DeSiaMore www.desiamore.com/ifm 9

10 Implementation of ADT An implementation of ADT consists of storage structures to store the data items and algorithms for basic operation DeSiaMore www.desiamore.com/ifm 10

11 Data Structures, Abstract Data Types, and Implementations in Real World Consider example of an airplane flight with 10 seats to be assigned Tasks  List available seats  Reserve a seat How to store, access data? DeSiaMore www.desiamore.com/ifm 11

12 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 DeSiaMore www.desiamore.com/ifm 12

13 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. DeSiaMore www.desiamore.com/ifm 13

14 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 DeSiaMore www.desiamore.com/ifm 14

15 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 DeSiaMore www.desiamore.com/ifm 15

16 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 DeSiaMore www.desiamore.com/ifm 16

17 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 DeSiaMore www.desiamore.com/ifm 17

18 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 DeSiaMore www.desiamore.com/ifm 18

19 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 DeSiaMore www.desiamore.com/ifm 19

20 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[] DeSiaMore www.desiamore.com/ifm 20

21 Multidimensional Arrays… 2D- Arrays A two dimensional array is a collection of elements placed in m rows and n columns. The syntax of 2-D array includes two subscripts, of which one represents number of rows and the other specifies the number of columns of an array These two subscripts are used to reference an element in an array. For example arr[3][4] is a 2-D array containing 3 rows and 4 columns. DeSiaMore www.desiamore.com/ifm 21

22 Multidimensional Arrays… 2D-Arrays Arr[0][2] is an element placed at 0 th row and 2 nd column in the array. The two dimensional array is also called a matrix. Representation of a 2-D array. 0123 0121-625 1307315634 24294256 DeSiaMore www.desiamore.com/ifm 22

23 Multidimensional Arrays … 2D- Arrays Consider a table of test scores for several different students DeSiaMore www.desiamore.com/ifm 23

24 Row Major and Column Major Arrangement Rows and columns of matrix are imaginary. Matrix are get stored in memory linearly as computer memory can be viewed as consecutive units of memory. This lead to two major arrangements in memory:  Row Major arrangement  Column major arrangement Consider an example below DeSiaMore www.desiamore.com/ifm 24

25 Row Major and Column Major Arrangement int a[3][4] = { { 12,1,-9,23} { 14,7,11,121} { 6,78,15,34} } DeSiaMore www.desiamore.com/ifm 25

26 Row Major and Column Major Arrangement Since the array elements are stored in adjacent memory locations we can access any elements of the array one we know the base address (starting address) of the array and number of rows and columns present in the array. Example if the base address of the array above is 502 and we wish to refer the element 121, then the calculation involved would be as follows: DeSiaMore www.desiamore.com/ifm 26

27 Row Major and Column Major Arrangement Row Major arrangement: Element 121 is present at a[1][3]. Hence location of 121 would be = 502 + 1*4+3 = 502 + 7 = 516 In general for an array a[m][n] the address of element a[i][j] would be: Base address + i*n+j DeSiaMore www.desiamore.com/ifm 27

28 Row Major and Column Major Arrangement Column Major arrangement: Element 121 is present at a[1][3]. Hence location of 121 would be: = 502 + 3*3 + 1 = 502 + 10 = 522 Generally for an address of an array a[m][n] the address of element a[i][j] would be DeSiaMore www.desiamore.com/ifm 28

29 Row Major and Column Major Arrangement Base address + j*m+i. Note the C++ permits only a Row Major arrangement, whereas, Pascal uses a Column Major Arrangement. DeSiaMore www.desiamore.com/ifm 29


Download ppt "Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore www.desiamore.com/ifm DeSiaMorewww.desiamore.com/ifm1."

Similar presentations


Ads by Google