Arrays
Array name means its base address.
1-dimensional array: -
Representation and storage: -
Int a [8]: - a is an array of 8 blocks of integer type
1000 1002 1004 1006 1008 1010 1012 1014
1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝑎[3]) = 1000⏟𝐵𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 + (3 − 0)⏟𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝑏𝑒𝑓𝑜𝑟𝑒 𝑎[3] × 2⏟𝑠𝑖𝑧𝑒 𝑜𝑓 𝑒𝑎𝑐ℎ 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 = 1000 + 6
General format access:
array [lower bound to upper bound]
Location (a[i]) = Base address + (i – lower bound)
× element size.
2-Dimensional array: -
Int a [4][3] = {1, 2, ______12}
Representation:
Int a [4][3] = {1, 2, ______12}
1 2 3 1 2 3 4 [1 2 3 4 5 6 7 8 9 10 11 12 ]
123 [
1 2 3 4 𝑎11 𝑎12 𝑎13 𝑎21 𝑎22 𝑎23 𝑎31 𝑎32 𝑎33 𝑎41 𝑎42 𝑎43 ]
Storage: -
Row major order
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝑎[3][2]) = 1000⏟ + {(3 − 1)⏟𝑛𝑜. 𝑜𝑓 𝑟𝑜𝑤 𝑏𝑒𝑓𝑜𝑟𝑒 𝑎[3][2] × 3⏟𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑖𝑛 𝑒𝑎𝑐ℎ 𝑟𝑜𝑤=𝑛𝑜. 𝑜𝑓
𝐵𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝑎[3][2])=1014
Column major order: -
{
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛(𝑎[3][2]) = 1000⏟ +𝑏𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 (2 − 1)⏟𝑛𝑜. 𝑜𝑓 𝑐𝑜𝑙𝑢𝑚𝑛 𝑏𝑒𝑓𝑜𝑟𝑒 𝑎[3][2] × 4⏟𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑖𝑛 𝑒𝑎𝑐ℎ 𝑐𝑜𝑙𝑢𝑚𝑛=𝑛
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝑎[3][2]) = 1012
General format access: -
A [lower bound-1 upper bound-1] [lower bound-2 upper bound-2]
Row major order: -
a[i][j] = Base address + {(i – lower bound-1) × no. of column + (j
– lower bound-2)} × Se
Column major order: -
a[i][j] = Base address + {(j – lower bound-2) × no. of row + (i –
lower bound-1)} × Se
no. of column = upper bound-2 – lower bound-2 + 1
no. of row = upper bound-1 – lower bound-1 + 1
Se = Size of element
3-dimensional array: -
Representation :
a [2][3][4] → represented as collection of two [3][4] 2-dimensional
array which is shown in below diagram.
Set of two [3][4] 2-dimensional array
Storage: -
Row major order wit 1st 2-D array in row major first & then 2nd 2-D array in row
major
General format:
a [lower bound-1 – upper bound-1] [lower bound-2 – upper bound-2]
[lower bound-3 – upper bound-3]
a[i][j][k] = base address + {(i –lower bound 1) × no. of row × no. of
column + (j – lower bound 2) × no. of row + (k – lower bound 3)} ×
size of element
Column major order with 1st 2-D array in row major first & then 2nd 2-D array in
row major
General format access:
a [lower bound-1 – upper bound-1] [lower bound-2 – upper bound-2]
[lower bound-3 – upper bound-3]
a[i][j][k] = base address + {(i –lower bound 1) × no. of row × no. of
column + (k – lower bound 3) × no. of column + (j – lower bound 2)} ×
size of element
Lower triangular matrix: -
Representation: -
[
1 2 3 4 1 2 3 4 𝑎11 0 0 0 𝑎21 𝑎22 0 0 𝑎31 𝑎32 𝑎33 0 𝑎41 𝑎42 𝑎43 𝑎44 ]
Total no of element in 4*4 array is 16. If we store only non-zero values
then no of element need to be stored is
𝑛(𝑛+1) 4×5
No. of non-zero element = 1 + 2 + 3 + 4 = 2
= 2
= 10
Storage:
Row major order: -
⎧
(3−1)(3−1+1)
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛(𝑎[3][2]) = 1000⏟𝑏𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 + { 2
)⏟ 𝑟𝑑
⎨ 𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑏𝑒𝑓𝑜𝑟𝑒 𝑎[3][2]𝑖𝑛 3 𝑟𝑜𝑤 (2)(2
𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑏𝑒𝑓𝑜𝑟𝑒 3𝑟𝑑 𝑟𝑜𝑤 2
⎩
=1008
General format access:
A [lower bound-1 upper bound-1] [lower bound-2 upper bound-2]
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝐴[𝑖][𝑗]) =0 if i<j
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝐴[𝑖][𝑗]) = ⎡𝐵𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 +
⎣ { (𝑖−𝑙𝑜𝑤𝑒𝑟 𝑏𝑜𝑢𝑛𝑑1)(𝑖−𝑙𝑜𝑤𝑒𝑟 𝑏𝑜𝑢𝑛𝑑1+1)
2
+ (𝑗 − 𝑙𝑜𝑤𝑒𝑟 𝑏𝑜𝑢𝑛𝑑2) ×𝑠𝑖 }
Column major order
⎰ 4(4+1) (4−2+1)(4−2+2)
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛(𝑎[3][2]) = 1000⏟𝑏𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 + ⏟ − ⏟
⎱ 2 𝑇𝑜𝑡𝑎𝑙 𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑖𝑛 𝑠𝑡𝑜𝑟𝑎𝑔𝑒
2
𝑁𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒
=1010
General format access:
A[lower bound-1 upper bound-1][lower bound-2 upper bound-2]
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝐴[𝑖][𝑗]) =0 if i<j
(𝑛𝑟)(𝑛𝑟+1)
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝐴[𝑖][𝑗]) = ⎡⎢𝐵𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 𝑠 +
⎣ 2{ −
(𝑢𝑝𝑝𝑒𝑟 𝑏𝑜𝑢𝑛𝑑 2−𝑗+1)(𝑢𝑝𝑝𝑒𝑟 𝑏𝑜𝑢𝑛𝑑 2−𝑗+2)
2
+ (𝑖 −
Where nr = no. of row = upper bound-1 – lower bound-1 + 1
Upper triangular matrix: -
Representation: -
1 2 3 [
4 1 2 3 4 𝑎11 𝑎12 𝑎13 𝑎14 0 𝑎22 𝑎23 𝑎24 0 0 𝑎33 𝑎34 0 0 0 𝑎44 ]
Total no of element in 4*4 array is 16. If we store only non-zero values
then no of element need to be stored is
𝑛(𝑛+1) 4×5
No. of non-zero element = 1 + 2 + 3 + 4 = 2
= 2
= 10
Storage:
Row major order: -
⎰ 4(4+1) (4−2+1)(4−2+2)
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛(𝑎[2][3]) = 1000⏟𝑏𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 + ⏟ − ⏟
⎱ 2 𝑇𝑜𝑡𝑎𝑙 𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑖𝑛 𝑠𝑡𝑜𝑟𝑎𝑔𝑒
2 𝑛𝑑
𝑁𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑎𝑓𝑠𝑡𝑒𝑟 2
=1010
General format access:
A [lower bound-1 upper bound-1] [lower bound-2 upper bound-2]
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝐴[𝑖][𝑗]) =0 if i>j
(𝑛𝑟)(𝑛𝑟+1)
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝐴[𝑖][𝑗]) = ⎡⎢𝐵𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 𝑠 +
⎣ 2{ −
(𝑢𝑝𝑝𝑒𝑟 𝑏𝑜𝑢𝑛𝑑 2−𝑖+1)(𝑢𝑝𝑝𝑒𝑟 𝑏𝑜𝑢𝑛𝑑 2−𝑖+2)
2
+ (𝑗 −
Where nr = no. of row = upper bound-1 – lower bound-1 + 1
Column major order
⎧
(3−1)(3−1+1)
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛(𝑎[2][3]) = 1000⏟𝑏𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 + { 2
)⏟ 𝑟𝑑
⎨ 𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑏𝑒𝑓𝑜𝑟𝑒 𝑎[3][2]𝑖𝑛 3 𝑟𝑜𝑤 (2)(2
𝑛𝑜. 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑏𝑒𝑓𝑜𝑟𝑒 3𝑟𝑑 𝑟𝑜𝑤 2
⎩
=1008
General format access:
A [lower bound-1 upper bound-1] [lower bound-2 upper bound-2]
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝐴[𝑖][𝑗]) =0 if i>j
𝐿𝑜𝑐𝑎𝑡𝑖𝑜𝑛 (𝐴[𝑖][𝑗]) = ⎡𝐵𝑎𝑠𝑒 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 +
⎣ { (𝑗−𝑙𝑜𝑤𝑒𝑟 𝑏𝑜𝑢𝑛𝑑1)(𝑗−𝑙𝑜𝑤𝑒𝑟 𝑏𝑜𝑢𝑛𝑑1+1)
2
+ (𝑖 − 𝑙𝑜𝑤𝑒𝑟 𝑏𝑜𝑢𝑛𝑑2) ×𝑠𝑖 }
Tip: Don’t just remember formula, understand the way it is stored &
using that always derive formula.