You are on page 1of 14

Lecture# 08

Data Structure & Algorithms

By
Engr. Mohammad Nawaz
nawazmeer@buetk.edu.pk
Lecturer at CSE&S Department
Balochistan UET khuzdar
Data Structure & Algorithms
CLO-1(PLO-1) 2
Previous Lecture Review
• Linear Array
– Declaration of an Array
– Initialization of an Array
– Length of an Array

Data Structure & Algorithms


CLO-1(PLO-1) 3
Today’s Lecture Outline:
• Memory Representation of Linear Array
– Index
– Element
– Physical Address
– Base Address
– Words per memory cell
– Length of an Array
• Examples
Data Structure & Algorithms
CLO-1(PLO-1) 4
Lecture Objectives
At the End of this Lecture You will be able to
• Understand the Structure of an Array
• Easily calculate the address of any location of an
array.

Data Structure & Algorithms


CLO-1(PLO-1) 5
REPRESENTATION OF LINEAR ARRAYS IN MEMORY

int data[4]={10,20,30,40};
Elements(values)

Indexes (Logical) Address (physical)

Data Structure & Algorithms


CLO-1(PLO-1) 6
REPRESENTATION OF LINEAR ARRAYS IN MEMORY
• The elements of LA are stored in successive memory cells. 01
100
• The computer does not need to keep track of the address 23
45
of each element of LA, 102

• But needs to keep track only the address of the first 104
element of LA, which is denoted by Base(LA)
• Using base address Base(LA), the computer calculates 106

the address of any element of LA by the following formula: 108


• LOC(LA[K]) = Base(LA) + w (K- lower bound) 110

Data Structure & Algorithms


CLO-1(PLO-1) 7
Formula
01
100
23
45
LOC(LA[K]) = Base(LA) + w (K- lower bound) 102

104

Address to be Searched Base Address 106

Words per memory cell 108

110

Data Structure & Algorithms


CLO-1(PLO-1) 8
Formula Verification
Suppose computer requires to find the address of A[3]
01
100
And words per memory cell w=2. 23
45
Solution: 102

Formula: LOC(LA[K]) = Base(LA) + w (K- lower bound) 104


LOC(A[3])= 100 + 2(3-0)
LOC(A[3])=100+6 106

LOC(A[3])=106 Result 108

110

Data Structure & Algorithms


CLO-1(PLO-1) 9
Examples
• Consider the following array X(11:50)
• Suppose Base(X)= 600 and there are w=4 words per memory location
find the address of X[20].
Solution:
Formula: LOC(LA[K]) = Base(LA) + w (K- lower bound)
LOC(X[20])=600+4(20-11)
LOC(X[20])=600+36
LOC(X[20])=636 Result

Data Structure & Algorithms


CLO-1(PLO-1) 10
Examples
• Consider the following array X(-11:40)
• Suppose Base(X)= 200 and there are w=2 words per memory
location find the address of X[20].
Solution:
Formula: LOC(LA[K]) = Base(LA) + w (K- lower bound)
LOC(X[20])=200+2(20-(-11))
LOC(X[20])=200+2(31)
LOC(X[20])=262 Result
Data Structure & Algorithms
CLO-1(PLO-1) 11
Independent Practice
Q1:
• Consider the following array X(11:50)
• Suppose Base(X)= 200 and there are w=4 words per
memory location find the address of X[10].
Q2:
• Consider the following array Z(-99:50)

• Suppose Base(Z)= 100 and there are w=2 words per


memory location find the address of Z[22].

Data Structure & Algorithms


CLO-1(PLO-1) 12
ANY QUESTION

Data Structure & Algorithms


CLO-1(PLO-1) 13
Data Structure & Algorithms
CLO-1(PLO-1) 14

You might also like