12,833 views

Consider the following declaration of a two-dimensional array in C:

char $a[100][100]$;

Assuming that the main memory is byte-addressable and that the array is stored starting from memory address $0$, the address of $a [40][50]$ is:

1. $4040$
2. $4050$
3. $5040$
4. $5050$

1 comment

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!!

Subscribe to GO Classes for GATE CSE 2022

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$.

Why column major used here,and not row major?
very well explained
i think by these ans may be contradict but by default we use  column majaor

.its a naive approach :)
why coloum major order plese explain because if we solve this from row major order then anwer will come out to be 5040 (Option C) which is also given in the option ?
Why element size is 1?
char --> 1
Who is using column major? It is row-major which is mandated in C.

@Arjun sir....any source plss

B is the answer, and RMO is used here not CMO.
Because here both answers are given, 1)CMO - 5040 , 2) RMO - 4050 then how to decide the correct one?

Shubhgupta Use row major .Use column major when it is exclusively mentioned in question.

Here it is used in row major order.
what are lb1 and lb2??

i think by these ans may be contradict but by default we use  column majaor

.its a naive approach :)

Any newbie please note that this information is incorrect.

Row Major Order is default in C (unrelated: Static scoping is also default in C)

$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$

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

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.

My man forgot about base address... And size of element, lower bound,But still not matter in this case

OPTION B

1
2
6,319 views
3