# Thread: Calculating the number of elements in a multidimensional array

1. ## Calculating the number of elements in a multidimensional array

how can i calculate the different between a point in a multidimensional array and the end of the array?

myarray[2][3][4] - myarray[0][2][2] = ___ elements

OLD QUESTION

let's say i have a multidimensional array with 24 elements: myarray[1][2][3]

[length of 2] x [length of 3] x [length of 4] = length of 24 total elements

well now let's say that i want to only populate the elements of the array from myarray[0][2][1] on...

i want to determine how many elements are between myarray[0][2][1] and myarray[1][2][3] (the full array).

2. simplified the question a bit

3. It still isn't very clear. Also, are you aware that Javascript does not support multidimensional arrays? Instead, objects can be used:
Code Javascript:
```var obj = {
1: [1, 2, 3],
2: ['a', 'b', 'c', 'd'],
3: {
1: [1, 2],
2: ['cat', 'dog']
}
}```
It looks like a multidimensional array because with objects you can use "array notation", so if you do this:
Code:
`obj[3][2][1]`
you get 'dog'. That is the second index of an array in an object within an object.

4. Here's some test code

Code Javascript:
```// create 2x3x4 array
myarray = Array(2);
for (var i = 0; i < myarray.length; i++) {
myarray[i] = Array(3);
for (var j = 0; j < myarray[i].length; j++) {
myarray[i][j] = Array(4);
}
}
// provide the distance from a location to the end of the array
function distance(x, y, z) {
var count = 0;
for (var i = x; i < myarray.length; i++) {
for (var j = y; j < myarray[i].length; j++) {
for (var k = z; k < myarray[i][j].length; k++) {
count++;
}
z = 0;
}
y = 0;
}
return count;
}
// [0,2,1], [0,2,2], [0,2,3]
// [1,0,0], [1,0,1], [1,0,2], [1,0,3]
// [1,1,0], [1,1,1], [1,1,2], [1,1,3]
// [1,2,0], [1,2,1], [1,2,2], [1,2,3]```

5. If your array has size x,y,z
then the distance between myarray[a][b][c] and the end of the array is

xyz - ayz - bz - c

*I think*

6. A 2x3x4 array has 24 elements
element 1x3x2 is at indexed position 0,2,1

x = 2, y = 3, z = 4
a = 0, b = 2, c = 1

xyz - ayz - bz - c
= 24 - 0 - 8 - 1
= 16 - 1
= 15

It seems right to me.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•