This line from question :

Assuming that the main memory is byte-addressable

I know what is byte-addressable. Can anyone tell what answer will come if it is word addressable. doesn’t able to get in what aspect they given here? anyone help!!

12,833 views

Best answer

The answer is (**B**).

In $\mathbb{C},$ arrays are always stored in the row-major form.

Formula to evaluate $2-D$ array's location is:

$loc(a[i][j]) = BA + [(i-lb_1)\times NC+(j-lb_2)]\times c$

where,

- $\text{BA}$ - Base Address
- $\text{NC}$ - no. of columns
- $c$ - memory size allocated to data type of array $a[lb_1 \ldots ub_1] [lb_2\ldots ub_2]$

Here, $\text{BA} = 0, \text{NC} = 100, c=1, a[0 \ldots 99][0\ldots 99]$, so $lb_1=0 , lb_2=0$

$loc(a[40][50])= 0+ [ (40-0)\times 100 + (50-0)]\times 1$

$\qquad \qquad \quad = 0+[4000+50]\times 1 = 4050$.

$a$ $\underbrace{[100]}$ $\underbrace{[100]}$

$\text{Streets}$ $\text{Buildings}$

Now you want to go and meet a friend who lives on $50^{th}$ building of $40^{th}$ street means $a[40][50]$

So, firstly cross $39 (0-39)$ streets each consist of $100$ buildings: $40\times 100=4000$

Now cross $49(0-49)$ buildings in order to reach your destination: $50$

$Ans: 4050$

$\text{Streets}$ $\text{Buildings}$

Now you want to go and meet a friend who lives on $50^{th}$ building of $40^{th}$ street means $a[40][50]$

So, firstly cross $39 (0-39)$ streets each consist of $100$ buildings: $40\times 100=4000$

Now cross $49(0-49)$ buildings in order to reach your destination: $50$

$Ans: 4050$

Good analogy! I always use "Skip i rows and j columns", thanks to you I'll add a story behind that from now on.

Elaborating your answer for completeness' sake.

Skip 40 rows and 50 columns

=> Skip 40*(Size of column) elements + 50 elements.

=> Skip 40*100 elements + 50 elements.

=> Skip 4050 elements.

Here, the element is char, and 1 char takes up 1 Byte.

So, 4050 Bytes. => 4050th address.

Also, note that this approach works when the initial element is at A[0][0]. If somewhere else like A[-5][2], then skip accordingly.

Search GATE Overflow