SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    501
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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).
    Last edited by NokX; Feb 4, 2008 at 15:29. Reason: simplify the question

  2. #2
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    501
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    simplified the question a bit

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    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;
    }
    alert(distance(0, 2, 1)); // alerts 15
    // [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]
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your array has size x,y,z
    and your element is myarray[a][b][c]
    then the distance between myarray[a][b][c] and the end of the array is

    xyz - ayz - bz - c

    *I think*

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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