Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Multiple Arrays

  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Multiple Arrays

    I have a quick question with multiple array and random numbers. If i generate my random numbers in one array, how would i take a selection of those numbers and put them in another array?

    Ex: array 1: 25, 34, 38, 40, 22, 49

    want to move numbers between 30 and 50 to another array.

    array 2: 34, 38, 40, 49


    is it as simple as for loops and if statements setting the conditions?
    do i use a sorting method? (selection? bubble?)

    any help would be appreciated.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,622
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Do you *WANT* them sorted in the other array?

    Or do you want them in the same order?

    There are a ton of ways to do this.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    well they dont have to be sorted from low to high they just have to be sorted into the specific array.
    just if the numbers fall between this range, then they go in this array.

    example to help clarify..
    array 1:1-10
    array 2: 11-20
    array 3 21-30

    array 4(random): 5, 12, 3, 2, 29,4,7

    The numbers to be organized are from the random array

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,622
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Ahhh...you have changed the question, so what I was thinking doesn't even matter.

    I thought you had only *ONE* destination array.

    If you have multiple destinations, then just the brute force approach will be best.
    Code:
    var source = [5, 12, 3, 2, 29,4,7];
    var to10 = [];
    var to20 = [];
    var to30 = [];
    var temp = [to10, to20, to30];
    
    for ( var i = 0; i < source.length; ++i )
    {
        var n = source[i];
        var decade = Math.floor( (n-1) / 10 );
        if ( decade >= 0 && decade <= 2 ) temp[ decade ].push( n );
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    a sufficiently modern browser can also use the filter method.
    PHP Code:
    var to10 source.filter(function (el) { return (el >  && el 11); });
    var 
    to20 source.filter(function (el) { return (el 10 && el 21); });
    var 
    to30 source.filter(function (el) { return (el 20 && el 31); }); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    alright so your using if with a && operator.

    I'll get to work and post my results later!

    Thanks

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,622
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Yeah, unless this is homework, and unless it has to work on older browsers, use Dormilich's answer.

    If it's homework, likely the prof won't believe you wrote either answer.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Dormilich View Post
    a sufficiently modern browser can also use the filter method.
    taking advantage of late-bound this can simplify and shorten:
    Code:
    function compare(el) { return el > this[0]  &&  el < this[1] ; }
    var to10 = source.filter(compare, [ 0,11]);
    var to20 = source.filter(compare, [10,21]);
    var to30 = source.filter(compare, [20,31]);
    this way, you can create more array slots without coding new filter functions for each range...
    Last edited by rnd me; 11-12-2010 at 01:53 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,622
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Oh, w.t.h. Wasn't going to do this, but...

    Since we are all getting in on the act...

    One advantage of the brute force method is that it can be self adapting.
    Code:
    <script type="text/javascript">
    var source = [5, 12, 3, 2, 29,4,7, 48, 41, 57, 22, 1, 4.3, 17.999 ]; // make this as big as you want, with values up to hundreds
    
    var decades = [ ];
    
    for ( var i = 0; i < source.length; ++i )
    {
        var n = source[i];
        var decade = Math.floor( (n-1) / 10 );
        if ( decades[decade] == null ) decades[decade] = [];
        decades[ decade ].push( n );
    }
    
    for ( var d = 0; d < decades.length; ++d )
    {
        if ( decades[d] != null )
        {
            document.write( "<hr>decade " + d + "::" + decades[d] );
        }
    }
    </script>
    Only limitation is that the numbers must be 1 or greater, because of how he defined the ranges.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Old Pedant View Post
    Oh, w.t.h. Wasn't going to do this, but...

    Since we are all getting in on the act...

    One advantage of the brute force method is that it can be self adapting.
    ...
    Only limitation is that the numbers must be 1 or greater, because of how he defined the ranges.

    good ideas.
    functional can also be self-adapting, and using an object permits negative decades:

    Code:
    var source = [
    5, 12, 3, 2, 29,4,7, 48, 41, 57, 22, 1, 4.3, 17,323, -6,-13,-18.4,-55,-75,-434 ]; 
    
    var decades= source.sort(function(a,b){return a-b;}).map(function(a,b){ 
        var bns= a>0? 0 : 1, 
        i= bns+Math.floor(a/10) || (bns? "-0" : 0),
        r= this[i] || (this[i]= []);
        r[r.length]= a;
        return this;
    },{})[0];
    
    
    alert(JSON.stringify( decades , null, "\t"));
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,622
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    That puts 0 into the "1" decade and puts all multiples of 10 into the wrong decade. Latter is easily fixed. I'm not clear why ends up in "1".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Old Pedant View Post
    That puts 0 into the "1" decade and puts all multiples of 10 into the wrong decade. Latter is easily fixed. I'm not clear why ends up in "1".
    DOH!

    that code only has problems with negative multiples of 10, didn't notice in my test case.

    fixes in green:
    Code:
    var source = [
    5, 12, 3, 2, 29,4,7, 48, 20,30,40,0,41, 57,60,64,-60,-64, 22, 1, 4.3, 17,323, -6,-13,-18.4,-55,-80,-75,-434 ]; 
    
    var decades= source.sort(function(a,b){return a-b;}).map(function(a,b){ 
        var bns= a>=0? 0 : 1, 
        negPad=   bns? 9e-15 : 0,
        i= bns+Math.floor((a/10)-negPad) || (bns? "-0" : 0),
        r= this[i] || (this[i]= []);
        r[r.length]= a;
        return this;
    },{})[0];
    
    alert(JSON.stringify( decades , null, "\t"));
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by Old Pedant View Post
    Oh, w.t.h. Wasn't going to do this, but...

    Since we are all getting in on the act...

    One advantage of the brute force method is that it can be self adapting.
    Two improvements:-

    var source = [5, 12, 3, 2, 29,4,7, 48, 41, 57, 22, 1, 4.3, 17.999 ];
    source.sort(function(a,b){return a - b});

    for ( var d = 0; d < decades.length; ++d ){
    if ( decades[d] != null ) {
    var list = decades[d].join("&nbsp&nbsp&nbsp");
    document.write( "<hr>decade " + d + ":&nbsp&nbsp&nbsp " + list);

    }

  • #14
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Question...

    if i document.write the array, shouldnt it show the newly indexed numbers?

    in my code, im just getting comas and it seems to be indexing the appropriate amount of commas for the random numbers but its just not showing it.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Whose code are you trying to use? Old Pedant's or rnd me's?
    I have to say that they both work just fine for me.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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