edited by
27,644 views
39 votes
39 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$
edited by

4 Answers

Best answer
54 votes
54 votes

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
23 votes
23 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$
0 votes
0 votes

you can use the formula:

Address = base address +(row index × number of columns + column index )× size of each element

So, the address of a[40][50] would be:

Address = 0 +(40×100+50)×1 = 4050

 

 

 

Answer:

Related questions

35 votes
35 votes
3 answers
2
Kathleen asked Sep 15, 2014
10,072 views
The C language is:A context free languageA context sensitive languageA regular languageParsable fully only by a Turing machine
13 votes
13 votes
2 answers
4
Kathleen asked Sep 15, 2014
8,177 views
The results returned by function under value-result and reference parameter passing conventionsDo not differDiffer in the presence of loopsDiffer in all casesMay differ i...