0% found this document useful (0 votes)
10 views

Array Class

Uploaded by

jainishramanuj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Array Class

Uploaded by

jainishramanuj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Array

One Dimensional Array


 Notation: A[l:u]
where, A =Array Name
l=lower bound, u=upper bound
 Example:
if int A[6] declared in C program means
A[0:5]
 In general, A[-3 : 2] it has 6 elements
where, l =-3
u=2
One Dimensional Array
 No of elements:
 (u – l + 1)
 Because elements are stored sequentially
in the memory we now derive formula to
find the address of an element in one
dimension array.

 If we want to find address of


A[i]
Address of an element in 1-D

A[ i ] = B.A.+(i ̶ l) × c
where,
i = index
B.A.= Base Address (OR Offset)
l = lower bound of an array
c = size of each element in bytes
(Scale Factor)
Example
 A[ -2 : 3 ] (array of int with BA=2001)
Find Address of A[2].
No of elements: (u – l + 1) = (3 - ( - 2 )
+1) = 6
l = -2, i =2
u = 3,
c = 4, BA= 2001
A[i] = B.A.+(i - l) × c
= 2001+(2 –(-2)) × 2
= 2001+(4 × 2)
= 2009
Two Dimensional Array
 Notation: A[ lr : ur , lc : uc ]

where,
A=Array Name
lr =lower bound for row, ur =upper bound
for row
lc =lower bound for col, uc =upper bound
for col
Calculate elements

 No of rows: r = (ur – lr + 1)
 No of columns: c = (uc – lc + 1)
 No of elements: r×c = (ur – lr + 1)×(uc – lc
+ 1)
Example (Row-Major Order)
 If D[-13:1, 4:9] is an array of float find the
address of D[-2, 8]. Address of D[-13,4] is
3000.
Here, lr =-13, ur =1, BA=3000, c=4(
lc =4, uc =9, i=-2, j=8

D[-2,8] = B.A.+[(i – lr)(uc – lc + 1)+( j– lc)]×c


= 3000 + [((-2)-(-13))(9-4+1)+(8-4)]
×4
= 3000 + [(11×6)+4] ×4
= 3000 + 280
= 3280
Column Major

 If D[-13:1, 4:9] is an array of float find the


address of D[-2, 8]. Address of D[-13,4]=3000.
Here, lr =-13, ur =1, BA=3000, c=4(
lc =4, uc =9, i=-2, j=8
D[-2,8] = B.A.+[(j– lc)(ur – lr + 1)+(i – lr)]×c
= 3000 + [(8-4)(1-(-13)+1)+(-2-(-13)] ×4
= 3284
 For given array of float A [-100:52,
60:89] find the total number of
elements. Assume that Base address
is 5001. Find address of A [0, 71]
element, if it is stored in row major
order.
Row-Major Order (Assume Array Index starts
with 0)

Here
Here Array of float A [-100:52, 60:89]
Example
 Given a two dimensional array Zl (3:l0,
10:20) stored in row-major order with base
address of 200 and size of each element of
4 bytes, find address of element Z1(5, 15).
 Given 2D array M[-12:15, 2:22] with base
address 1001 and type of the element is
float. Find the address of element M[-7,20]
using row major and column major order.
Gate 2004
Two matrices M1 and M2 are to be stored in arrays A and B
respectively. Each array can be stored either in row-major or
column-major order in contiguous memory locations. The time
complexity of an algorithm to compute M1 × M2 will be
(A) best if A is in row-major, and B is in column- major order
(B) best if both are in row-major order
(C) best if both are in column-major order
(D) independent of the storage scheme
Answer: (D)

Two matrices M1 and M2 are to be stored in arrays A and B


respectively. Each array can be stored either in row-major or
column-major order in contiguous memory locations. The time
complexity of a program to compute M1 × M2 will be
Answer : (A) 18
Three Dimensional Array
 A three dimensional array is a collection of
two dimensional arrays (planes). Each two
dimensional array contains rows and
columns.
 For example,
Three Dimensional Array
 Notation: A[ lP : uP, lr : ur , lc : uc]
where, A =Array Name
lP =lower bound for plane, uP =upper bound for
plane
lr =lower bound for row, ur =upper bound for row
lc =lower bound for col, uc =upper bound for col

 A[2][3][4] means two 2-D arrays


having size 3×4.
 Here, a 2-D array is denoted
as a plane
Three Dimensional Array
 No of Planes: p = (uP – lP + 1)
 No of rows: r = (ur – lr + 1)
 No of columns: c = (uc – lc + 1)
 No of elements: p×r×c
= (uP – lP + 1)×(ur – lr + 1)×(uc
– lc + 1)

Example: A[-1:1, 2:4, -10:-6]


No of Planes: (1)-(-1)+1=3
No of rows: (4)-(2)+1=3
No of columns: (-6)-(-10)+1=5
Memory Representation
 For multi-dimensional, two types of
memory representation is possible.

1. Row major:

2. Column major:
Find the address of an element in
3-D
 Stored in Row major:

No of elements in all
previous rows=
( j – lr)(uc – lc +1)

No of elements in
current row=(k – lc)
For Row Major & Column Major
Example
 Row Major:
A[i,j,k] =B.A.+[(i – lP)(ur – lr +1)(uc – lc +1)+( j – lr)(uc – lc +1)+(k – lc)]×c
= 140+[(0-(-2))((-1)-(-4)+1)(3-1+1)+((-2)-(-4))(3-
1+1)+(2-1)]×2
= 140+[(2)(4)(3)+(2)(3)+1]×2
= 140+[31]×2 = 202

 Column
A[i,j,k] =B.A.+[(iMajor:
– lP)(ur – lr +1)(uc – lc +1)+ (k – lc)(ur – lr +1)+( j – lr)]×c

= 140+[(0-(-2))((-1)-(-4)+1)(3-1+1)+(2-1))((-1)-(-4)+1)+
((-2)-(-4)]×2
= 140+[(2)(4)(3)+(1)(4)+2] ×2
= 140+[30] ×2 = 200

You might also like