# Two dimensional array

2k views
Consider a 2 dimensional array A[40...95,40...95] in lower triangular matrix representation. The size of each element of array is 1 Byte.If array is implemented in memory as Row major,with base address as 1000,the address of A[66][50] is .....

Ans. 1361
in DS
0
Please explain general methodology for solving it :)
0
please represent using pen and paper that how it is represented in matrix ...im not able to visualize it.

In general for RMO we calculate like :

Let A[LR.....UR][Lc.....Uc]  be a 2D array

Address of A[i][j]= Base + Size of each element { (no. of rows fully covered before reaching row i x no. of columns in each row) + no. of elements covered in the current row i}

no. of rows fully covered = (i-LR)

no. of columns in each row= (Uc-Lc+1)

But here the case is a bit different. The elements are arranged in lower triangular matrix form like this:

The rows are not fully covered.

The 1st row has 1 element.

The 2nd row has 2 elements. The 3rd has 3 elements and so on.

So kth row will have k elements.

No. of elements covered upto k rows is 1+2+3....k = k(k+1)/2.

We need to find address of A[i][j]. To reach this row no. i we have to cover previous (i-LR) rows.

So, no. of elements covered in (i-LR) rows is ((i-LR)* (i-LR+1))/2 rows
Now we are at row no. i.

No. of elements covered in this row no. i is (j-Lc)  elements.

Address of A[i][j]= Base + size( ((i-LR)* (i-LR+1))/2  + (j-Lc))

Address of A[66][50] = 1000 + 1 ( (66-40)*(66-40+1)/2 + (50-40) ) = 1000 + (26*27/2 + 10 ) = 1000 + (351+10) =1361

------------------------------------------------------------------------------------------------------

We need to find address of A[66][50]. To reach this row no. 66 we have to cover (66-40)= 26 rows.

So, no. of elements covered in 26 rows = 26*(26+1)/2 = 351

Now we are at row no. 66.

No. of elements covered in this row is (50-40) = 10.

Address of A[66][50] = 1000 + (351+10) =1361

selected by
0
@MINIPanda can you please explain the normal formula approach for  both row and column major,i.e row major : B+w{n(i-L1)+(j-L2)) and column major : B+w{n(j-L2)+(i-L1)}  how to access these
0
is this type of questions are present in gate ?
0
will u give some pictorial representation for the given array?
0

@suneetha The one that is already given in the question..

0
I think it should be $1362$. there are $66-40+1$ rows before $A[66]$. Shouldn't it be?
0

@2019_Aspirant

How many rows are there before A[41]? 41-40=1 row isn't it? Similarly, there are (i-40) rows before A[i].

0

@MiNiPanda Okay, thanks. sorry to disturb you on the trivial thing. seems I was outta my mind.

0
Its okay :)
1 vote

A[40 ..... 95, 40 ..... 95]

A[66] [50]

Total elements in rows = 95-40+1 = 56

total no of elements in columns = 95-40+1 = 56

base address = 1000   element size = 1

lower triangular matrix

1000 + ((66-40) (66-40+1)/2 + (50-40)) * 1

= 1000 +  361 = 1361

formula =     BA + ((i-lb1) (i-lb1 +1) /2 + (j-lb2)) * c (c is element size)

lb1 = lower bound of row    lb2 = lower bound of column

edited
0

1000 + ((66-40) (66-40+1)/2 + (50-40)) * 1

why you divided this by 2? @Deepak Raj 1

0
That's The Formula... In a overlook i didn't observe now I edited
1 vote
$a[40....95][40....95]$

$BA=1000$

$Size\ of\ element=1\ B$

$Loc[66][50]$

$\\ =1000+\left \{\underbrace {66-40=26}+(50-40) \right \}\times 1B$

$Natural\ \#\ sum\\ of\ 26\ rows$

$=1000+\left \{ \dfrac{26\times 27}{2}\ +\ 10 \right \}\times 1B$

$=1000+\left \{ 351+10 \right \}\times 1B$

$=1361B$
1 vote
$a[40...95,40...95]=a[56][56]$

$loc[66][50]:RMO$

$40^{th}\ row=1$

$41^{st}\ row=2$

$42^{nd}\ row=3$

.

.

.

$66^{th}\ row=26$

$Sum=1+2+3....+26=351$

Now you are standing on $66^{th}$ row and it's first element will start from $40^{th}$ column but you need to go to $50^{th}$ column, hence add $11(40,41,42....50)$

$351+11=362$

$362+1000=1362$

But index starts from $0,$ so subtract $1$

$1362-1=1361$
I think answer should be 2466 because given array is 2 dimensional then doesn't matter it is lower triangular,upper triangular or diagonal matrix as all 0's also require 1 B of memory,hence the address of A[66][50] will be:

1000+[(50-40)+(95-40+1)*(66-40)] = 2466
Yes it is 1361
$a[40...95,40...95]$

$loc[66][50]:RMO$

Make changes to approach in a different way.

$a[0...55,0...55]$

$loc[26][10]:RMO$

$0^{th}\ row=1$

$1^{st}\ row=2$

$2^{nd}\ row=3$

.

.

.

$25^{th}\ row=26$

$Sum=1+2+3....+26=351$

Now you are standing on $26^{th}$ row and it's first element will start from $0^{th}$ column but you need to go to $10^{th}$ column, hence add $10(0,1,2....9)$

$351+10=361$

$361+1000=1361$

## Related questions

1
324 views
Consider the following program: main (){ int i, j; int A[p][q] = { {1, 3, 5}, {2, 4, 6} }; for (i = 0; i < m; i++) for (j = 0; j < n; j++) printf("%d", * (A[j] + i)); } Assume $m$ is number of rows and $n$ is number of columns. Find the OUTPUT printed by the above program. $123456$ $135246$ $456456$ $456789$