Arrays
Arrays
Arrays
Arrays
• Data structures are either linear or non-linear
• Elements in a linear structure form a sequence.
• There are two ways of representing linear
structures in memory .
– One way is to have linear relationship between the
elements represented by means of sequential
memory locations. These linear structure are called
arrays
– Another way is to have linear relationships between
elements represented by means of pointers or links.
These linear structures are called linked lists.
• The non-linear structures are trees and graphs
Arrays
• An Array is the simplest data structure that
can be used in a program
• Arrays are data structures that contain
data items of the same type.
• Arrays are static . This means that the size
of the array is fixed from the declaration all
the way to the final program
implementation.
Why do we need Arrays in our
programming?
• For example if we wanted to keep a record
of personal expenditure for year 2011, you
can file all your receipts and details of
expenditure in multiple files according to
the month.
• This means that you have 12 different
files.
Why do we need Arrays in our
programming?
• However, wouldn’t it be easier if you could
manage a single file with 12 compartments?
• In computer programming if we were to
computerize the above, we could either
declare 12 separate variables to represent
the total monthly expenditure or even better
still we could declare an array with 12
elements
Arrays
• an array declaration is similar to normal
variable
• In general, we can declare one-
dimensional arrays as follows:
• data_type
array_name[number_of_elements];
• data_type - type of data that will be stored in the array
• array_name - the name of the array
• number_of_elements - a positive integer that indicates how many elements should
be stored in memory
Arrays
• A one-dimensional array is an array that
has only one index or subscript.
• An index is a value that is written in the
brackets [ ] after an array name.
• An index can identify the number of
elements in an array.
Arrays
• Some notations
Expense[0]
Expense[1]
Expense[2]
Expense[9]
array index
The memory of a computer is a
sequence of address locations
Linear Data Structures
• In memory if LA is a linear array then:
• LOC(LA[K]) = address of element LA[K] of
the array LA.
• Elements of array LA are stored in
successive memory cells.
• The computer only needs to keep track of
the first element of LA denoted by
Base(LA) called the base address of LA.
Linear Data Structures
}
}
202 }
}
203 }
}
204 HOTEL[2]
}
205 }
}
206 }
Example:
• Given the memory address of the 1st
element –> BASE address = 200 , Lower
Bound = 0, and the number of words per
memory cell (w) = 4
The address of the 3rd element in memory
of the following array structure Hotel is
= Base address + w(K-Lower Bound)
where K is the Nth element (i.e. N-1 = 2)
= 200 + 4(2-0) = 208
Linear Data Structures
• LOC(HOTEL[0]) = 200,
• LOC(HOTEL[1]) = 204,
• LOC(HOTEL[2]) = 208 ….
• LOC(HOTEL[29]) = ??
• A collection A of data elements is said to
be indexed if any element of A which we
will call Ak can be located and processed
in a time that is independent of K
Linear Data Structures
• Operations that are performed on linear structures
whether it be an array or linked list include:
0 0 0 Brown 0 Brown
Brown Brown
1 Davis 1 Davis 1 Davis 1 Ford
2 Johnson 2 Ford 2 Ford 2 Johnson
3 Smith 3 Johnson 3 Johnson 3 Smith
4 Wagner 4 Smith 4 Smith 4 Taylor
5 5 Wagner 5 Taylor 5
Wagner
6 6 6 Wagner 6
7 7 7 7