Lecture 3 – Arrays and ADT
Download
Report
Transcript Lecture 3 – Arrays and ADT
Data Structure and
Algorithm: CIT231
Lecture 3:
Arrays and ADT
DeSiaMore
www.desiamore.com/ifm
DeSiaMore
www.desiamore.com/ifm
1
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
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
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:
The collection of data items
Basic operation that must be performed on them
1.
2.
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.
www.desiamore.com/ifm
DeSiaMore
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
Tasks
List
available seats
Reserve 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 seats
Reserve a seat
How to store, access data?
10
DeSiaMore
individual variables
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
DeSiaMore
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
.
.
.
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 seats
Reserve a seat
How to store, access data?
10
individual variables
An array of variables
DeSiaMore
www.desiamore.com/ifm
14
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
www.desiamore.com/ifm
DeSiaMore
number is occupied
15
ADTs
In this simple example, it does illustrate the
concept of an Abstract Data Type
ADT consists of
The collection of data items
Basic operation that must be performed on
them
1.
2.
In the example, a collection of data is a list of
seats
The basic operations are (1) Scan the list to
determine whichwww.desiamore.com/ifm
seats are occupied (2)
DeSiaMore
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 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
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 array
Ordered-------------------------indices are numbered
0,1,2,…,capacity-1
Same type of elements-------specify the element type
20
www.desiamore.com/ifm
DeSiaMore
Direct
access-------------------subscript
operator[]
Multidimensional Arrays… 2DArrays
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
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.
0
1
2
3
0
12
1
-6
25
1
30
73
156
34
2
4
29
42
56
Representation of a 2-D array.
DeSiaMore
www.desiamore.com/ifm
22
Multidimensional Arrays … 2DArrays
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 arrangement
Column 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
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