Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMore .
-
Upload
marianna-hopkins -
Category
Documents
-
view
222 -
download
0
description
Transcript of Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMore .
Data Structure and Algorithm: CIT231
Lecture 3: Arrays and ADT
DeSiaMorewww.desiamore.com/ifm
DeSiaMore www.desiamore.com/ifm 1
Function for Deleting Array
void array::del (int pos){
//skip to the desired positionfor (int i=pos; i<MAX; i++)arr[i-1] = arr[i];arr[i-1]=0;
}
DeSiaMore www.desiamore.com/ifm 2
Displaying the array
void array::display (){
cout<<endl;//Traverse the entire arrayfor (int i = 0; I < MAX; i++)
cout<<“ “<<arr[i];}
DeSiaMore www.desiamore.com/ifm 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
DeSiaMore www.desiamore.com/ifm 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
DeSiaMore www.desiamore.com/ifm 5
DeSiaMore www.desiamore.com/ifm 6
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
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
Real world ADT example
DeSiaMore www.desiamore.com/ifm 9
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
Data Structures, Abstract Data Types, and Implementations in Real World
Consider example of an airplane flight with 10 seats to be assigned
TasksList available seatsReserve a seat
How to store, access data?
DeSiaMore www.desiamore.com/ifm 11
Data Structures, Abstract Data Types, and Implementations Consider example of an airplane flight with
10 seats to be assigned Tasks
List available seatsReserve a seat
How to store, access data?10 individual variables
DeSiaMore www.desiamore.com/ifm 12
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 false2. If seat1 ==‘ ’:
print “do you want seat #1??”Get answerif answer==‘Y’:set seat1 to ‘X’set Done to True
3. If seat2 ==‘ ’:print “do you want seat #2??”Get answerif answer==‘Y’:set seat2 to ‘X’set Done to True
.
.
.
DeSiaMore www.desiamore.com/ifm 13
Data Structures, Abstract Data Types, and Implementations Consider example of an airplane flight with 10
seats to be assigned Tasks
List available seatsReserve a seat
How to store, access data?10 individual variablesAn array of variables
DeSiaMore www.desiamore.com/ifm 14
Use Array Algorithm to List available seatsFor 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 reservedIf 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
ADTs In this simple example, it does illustrate the concept of
an Abstract Data Type
ADT consists of1. 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
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
Array ADT
Collection of data elementsA fixed-size sequence of elements, all of the
same type
Basic OperationsDirect 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
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
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 arrayOrdered-------------------------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
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
Multidimensional Arrays… 2D-Arrays Arr[0][2] is an element placed at 0th row
and 2nd column in the array. The two dimensional array is also called a matrix.
Representation of a 2-D array.
0 1 2 3
0 12 1 -6 25
1 30 73 156 34
2 4 29 42 56
DeSiaMore www.desiamore.com/ifm 22
Multidimensional Arrays … 2D-Arrays Consider a table of test scores for
several different students
DeSiaMore www.desiamore.com/ifm 23
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 arrangementColumn major arrangement
Consider an example below
DeSiaMore www.desiamore.com/ifm 24
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
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
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
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
Row Major and Column Major ArrangementBase 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