Chapter 2 Arrays
Chapter 2 Arrays
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
• A linear array is a list of finite number n of homogeneous
data elements such that:
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
• Let LA be a linear array
• The elements of LA are stored in successive memory
cells. So the computer does not need to keep track of the
address of every element of LA, but needs to keep track
only of the address of the first element of LA denoted by:
Base(LA)
• 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
Q1. Consider the linear arrays AAA(5:50), BBB(-5:10), and
CCC(18)
a) Find the number of elements in each array
b) Suppose Base(AAA)=300 and w=4 for AAA. Find the address
of AAA[15], AAA[35] and AAA[55]
Q2. Consider the array AUTO, which records the number of
automobiles sold each year from 2000 through 2006. The
Base(AUTO)=200, and w=4 words per memory cell for
AUTO.
c) Find the address of AUTO[2004].
Arrays in Python and java
• How to declare an array python?
The general form of declaring a simple (one dimensional) array is.
array_type variable_name [array size];
In your python program you can declare an array like
a=Array [10]=[1,2,3,4,5…..10];
• How to initialize an array C++?