The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
1.7k 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$
asked in Programming by Veteran (59.5k points)
edited by | 1.7k views

2 Answers

+25 votes
Best answer

Answer is (B).

In C, arrays are always stored in 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,

$BA$ - Base Address
$NC$ - no. of columns
$c$ - memory size allocated to data type of array

          $a[lb_1 \cdots ub_1] [lb_2\cdots ub_2]$

Here, $BA=0,  NC  =100,    c=1,    a[0.....99][0......99]$ so $lb_1=0 , lb_2=0$

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

answered by Active (3.3k points)
edited by
+2
Why column major used here,and not row major?
0
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
+4
Who is using column major? It is row-major which is mandated in C.
0

@Arjun sir....any source plss

+2
B is the answer, and RMO is used here not CMO.
0
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 votes

OPTION B

answered by Boss (19.8k points)


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

37,111 questions
44,694 answers
127,237 comments
43,753 users