in Programming edited by
12,833 views
27 votes

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$
in Programming edited by
12.8k views

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

0

Subscribe to GO Classes for GATE CSE 2022

3 Answers

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

edited by

14 Comments

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

.its a naive approach :)
0
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 ?
0
Why element size is 1?
0
char --> 1
0
Who is using column major? It is row-major which is mandated in C.
14

@Arjun sir....any source plss

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

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

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

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)

1
13 votes
$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$
edited by

2 Comments

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.

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

OPTION B

Answer:

Related questions

Ask
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true