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

Data Structures Notes

This document discusses how to store and access elements in multi-dimensional arrays. It explains row-major and column-major storage, and provides formulas to calculate the memory address of elements in n-dimensional arrays based on their indices and the array dimensions.

Uploaded by

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

Data Structures Notes

This document discusses how to store and access elements in multi-dimensional arrays. It explains row-major and column-major storage, and provides formulas to calculate the memory address of elements in n-dimensional arrays based on their indices and the array dimensions.

Uploaded by

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

Multi-Dimensional Arrays:

Suppose we have a 2D array Data with dimensions X=4 and Y=4 with the
following values:

We can access elements by writing Data[x][y] where x=0, 1,…,X-1 and


y=0,1,…Y-1

The total memory taken up by a 2D array can be calculated with:


X * Y * sizeof(data-type)

There are two ways of storing a 2D array in memory, Row major and Column
Major. In Row major, values are stored row after row:
To access the values of the elements the values of x and y would change like
this:

The memory allocation of arrays is contiguous, meaning that there will be no


gap between elements in memory.

Formula for calculating address of elements in a 2D array with dimensions X and


Y is:
&Data[x][y] = BA + (x * Y + y) * sizeof(data-type)

In the above formula, everything is the same as the 1D array’s formula except
“(x * Y + y)”. This part of the formula is used to calculate the slot number of the
elements.

These are the slot


numbers in this array
How to calculate slot numbers for multi-dimensional arrays:

Suppose we have a 4-digit decimal


number:

Each digit has 10 possible values ranging from 0 – 9


If we want to represent the combination of the first two digits in one cell we’ll
write:
d1 * 10 + d0
This is because 10 increments of the first digit result in one increment of the
second digit.
Eg. If d1 = 2 and d0 = 4, This means that d0 has made 2 full cycles from 0-9. So,
using the formula we get: 2 * 10 + 4 = 20 + 4 = 24. With this formula we can give
two digits individually and get a single number that is a combination of both
digits.

Now if we add another digit d2, the formula becomes:


d2 * 100 + d1 * 10 + d0

This is because d2 increases by 1 when d1 has made 10 increments. One


increment of d1 means 10 increments of d0. So, 10 * 10 = 100 increments in
total to increment d2 by 1.

Similarly, if we add a 4th digit, the formula becomes:


d3 * 1000 + d2 * 100 + d1 * 10 + d0

These are the significances of the digits:


We can represent array indexes in a similar way. d0 can be considered Y and d1
can be considered X.

If we have an array with dimensions [X][Y], we can calculate the slot numbers
for the elements using the formula:
xY + y, where x = 0, … , X-1 and y = 0, … , Y-1

If we add another dimension W, the new formula will be:


wXY + xY + y, where w = 0, … , W-1

Now the generalized formula for calculating the memory address of an element
in an n-dimensional array is:
Base address + (slot number formula) + sizeof(data-type)

Suppose we have an n-dimensional array Data[S0][S1][S2]…[Sn-1] where S0,


S1, S2, … Sn-1 are the dimensions of the array.

To access an element from the array we can write Data[I0][I1]…[In-1}


Where I0 = 0, 1…, S0-1 , I1 = 0, 1…, S1-1 and so on.

The formula for the slot number in an n-dimensional array basically follows the
rule that each index I0,I1,I2…In-1 depends on the sizes (S0,S1…) that come
before it. So In-1 doesn’t depend on anything, In-2 depends on Sn-1, In-3
depends on Sn-1 and Sn-2 and so on.
So the general formula to calculate slot number will be:
(I1 * S2 * S3 … * Sn-1) + (I2 * S3 * S4 … Sn-1) + (I3 * S4 * S5 … Sn-1) … (I1 *
Sn-1) + (In-1)

This can be further simplified to:

n−1 n−1
∑ 𝐈𝑗 ∗ ∏ 𝐒𝑘
𝑗=0 𝑘=𝑗+1

You might also like