0

Can some explain me the row & column wise representation of a 2 dimensional array in stack? My teacher told that if we have following matrix:

a00           a01           a02
a10           a11           a12
a20           a21           a22

Column wise representation:                     Row Wise representation:
a00                                             a00
a10                                             a01
a20                                             a02
a01                                             a10
a11                                             a11
a21                                             a12
a02                                             a20
a12                                             a21
a22                                             a22

Whereas i only know about the representation of multidimensional array in memory: a00 then a01 then a02 then a10 and so on(there increasing order of addresses)

I raised this question in class what is the difference b/w stack representation & memory representation of multidimensional arrays. She said we are doing 2-D array here not pointer. What kind of answer is that. Please explain me this.

She also told some formulae to calculate the address of any element of 2-D array of row representation and column representation in stack. I didn't understand it.

Location(A[j,k]) = Base_address(A) + W(M(k-1)+(j-1))

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Without code, "stack" is irrelevant to this question, (and a purist would tell you it isn't relevant even *with* code). – WhozCraig Sep 09 '14 at 19:47

2 Answers2

1

Here's a better representation of your 2D array in the RAM:

Column wise representation:

Chip1 Chip2 Chip3
a00   a01   a02
a10   a11   a12
a20   a21   a22

Row Wise representation:

Chip1 Chip2 Chip3
a00   a10   a20
a01   a11   a21
a02   a12   a22
CMPS
  • 7,733
  • 4
  • 28
  • 53
1

You said,

Whereas i only know about the representation of multidimentional array in memory: a00 then a01 then a02 then a10 and so on(there increasing order of addresses)

In C/C++, multidimensional arrays are stored using the row representation.

IIRC, in FORTRAN, multidimensional arrays are stored using the column representation.

In C, you can define a 2D array as:

int a[10][3];

When you pass the array to a function, it decays to a pointer of type int (*)[3].

Disclaimer: My FORTRAN is rusty, so pardon any use of incorrect syntax

In FORTRAN, you can define a 2D array as:

INTEGER A(10, 3)

When you pass the array to a function, it the argument type in the function looks like

INTEGER A(10, *)

The differences in the syntax makes it more natural for multidimensional arrays in C to be represented by rows while in FORTRAN it seems natural for them to be represented by columns.

You also said:

Location(A[j,k]) = Base_address(A) + W(M(k-1)+(j-1))

It seems you are using 1-based index. Not sure what W and M stand for.

Let's say you have ROW number of rows and COL number of columns.

If you have row representation:

Location(A[j,k]) = Base_address(A) + (j-1)*COL + (k-1)

If you have column representation:

Location(A[j,k]) = Base_address(A) + (k-1)*ROW + (j-1)
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • multidimensional arrays are always represented row wise not column wise? So i guess my teacher told it wrong we are studying algorithm using C – Stack Overflow 32 Sep 09 '14 at 20:17
  • 1
    @StackOverflow32, yes, that is correct. I would clarify with her. Maybe there was some miscommunication somewhere. – R Sahu Sep 09 '14 at 20:19