Professional Documents
Culture Documents
Structure:Arrays
Physical Memory
• We have to store our data structures in the memory of our computer.
• Memory is organized into banks, rows, columns etc.
• We supply a bank number, row number etc (= an address), memory
returns us the contents
• Address → contents
Physical Memory
Arrays
• The array is the most commonly used data storage structure; its built
into most programming languages.
• Since arrays are usually easy to traverse, search and sort, they are
frequently used to store relatively permanent collection of data.
• But if the size of the structure and the data in the structure is
constantly changing the array may not be useful.
• The elements of the array may be denoted by the bracket notation
A[0],A[1], A[2], A[3]…….A[n]
Linear Array
Length= UB-LB+1
where:
UB is the largest index, called the upper bound
LB is the smallest index, called the lower bound
Representation of Linear Arrays in Memory
• Using this address the computer can calculate the address of any
element of LA by the following formula:
LOC(LA[k])=Base(LA)+w(K-LB)
where:
K is the array index
W is the width of the record
Representation of Linear Arrays in Memory
Exercise
• Here LA is a linear array with lower bound LB and upper bound UB.
This algorithm traverses LA applying a PROCESS to each element of
LA.
a) Find the number NUM of years during which more than 300
automobiles were sold.
b) Print each year and the number of automobiles sold in that year.
Inserting and Deleting
[2,1] [1,2]
C1 r1
[3,1] [1,3]
[1,2] [1,4]
[2,2] [2,1]
C2 [2,2]
[3,2]
r2
[1,3] [2,3]
[2,3] C3 [2,4]
[3,3] [3,1]
[1,4] [3,2]
[3,3] r3
[2,4] C4
[3,4] [3,4]
Matrix Multiplication