Domanda

Suppose an array is declared as A[6][7][3][4], but by mistake someone references the cell A[4][9][3][2]. Which cell is accessed in actuality, in a language that is row major and indices starting at 0? Can anyone point me in the right direction for learning how to do this? I missed a day of class so I'm trying to search for how this is done and not having any luck. All I know is it has something to do with calculating an offset.

È stato utile?

Soluzione

First of all: Most modern languages will simply throw an index out of bounds exception. This is a good thing.

Now: A[4][9][3][2] has a problem in the middle two indices. We need to start with the first:

  • A[4][9] is A[4+(9/7)][9%7] which is A[5][2]
  • So A[4][9][3] is A[5][2][3] is A[5][2+3/3][3%3] which is A[5][3][0]
  • This makes A[4][9][3][2] map to A[5][3][0][2]

To understand, why throwing is a good idea, think of A[7][8][4][5].

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top