• edited by
39,058 views
60 60 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$

8 Answers

Best answer
76 76 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
46 46 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 2 votes


Address(a[i][j]) = Base + (i * COL + j) * size

Step Tree:
1. size = sizeof(datatype)  --> here char = 1
2. COL = given second dimension (100)
3. Substitute i=40, j=50
4. Address = (40*100 + 50)*1 + 0 = 4050

So final = 4050

 

Answer:
Position:
Show:

Related questions

63 63 votes
2 answers 2 answers
14.9k
14.9k views
Kathleen asked Sep 15, 2014
14,868 views
In the C language:At most one activation record exists between the current activation record and the activation record for the mainThe number of activation records betwee...
59 59 votes
3 answers 3 answers
17.4k
17.4k views
Kathleen asked Sep 15, 2014
17,403 views
The C language is:A context free languageA context sensitive languageA regular languageParsable fully only by a Turing machine
32 32 votes
2 answers 2 answers
6.2k
6.2k views
Kathleen asked Sep 15, 2014
6,151 views
The following recursive function in C is a solution to the Towers of Hanoi problem.void move(int n, char A, char B, char C) { if (......................) { move (...........
14 14 votes
2 answers 2 answers
11.4k
11.4k views
Kathleen asked Sep 15, 2014
11,443 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...