PDA

View Full Version : Multiple Arrays

Klmpee
11-10-2010, 10:15 PM
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.

Old Pedant
11-10-2010, 10:38 PM
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.

Klmpee
11-11-2010, 02:25 AM
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

Old Pedant
11-11-2010, 02:57 AM
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.

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 );
}

Dormilich
11-11-2010, 11:12 AM
a sufficiently modern browser can also use the filter method.

var to10 = source.filter(function (el) { return (el > 0 && el < 11); });
var to20 = source.filter(function (el) { return (el > 10 && el < 21); });
var to30 = source.filter(function (el) { return (el > 20 && el < 31); });

Klmpee
11-11-2010, 01:58 PM
alright so your using if with a && operator.

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

Thanks

Old Pedant
11-11-2010, 07:25 PM
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.

rnd me
11-12-2010, 12:49 AM
a sufficiently modern browser can also use the filter method.

taking advantage of late-bound this can simplify and shorten:

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...

Old Pedant
11-12-2010, 01:24 AM
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.

<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 );
}

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.

rnd me
11-12-2010, 02:28 AM
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:

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];

Old Pedant
11-12-2010, 02:46 AM
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".

rnd me
11-12-2010, 03:16 AM
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:

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];

Philip M
11-12-2010, 09:16 AM
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);
}

Klmpee
11-12-2010, 01:35 PM
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.

Philip M
11-12-2010, 01:57 PM
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. :)

Klmpee
11-12-2010, 02:21 PM
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. :)

Well im using neither code.. I just took ideas from both and started writing my own... Right now im debugging my code so it writes the values of the array..

*~Edit~*
So heres a code snippet

<script language="JavaScript">
var myArray= new Array(125);
var myA=new Array(125);
var myB=new Array(125);
var myC=new Array(125);
var myD=new Array(125);
var myF=new Array(125);
var i;

//generate scores to be sorted to other arrays
for(i=0;i<myArray.length; i++)
{ //random number generator. multiple difference with upper and lower value then add the low value.
myArray[i]=Math.round(Math.random()*(100-40)+40);
//Start Organizing grades into appropriate arrays
/*if(myArray[i]>=90 && myArray[i]<=100) //Use and operator to set parameter of which grades to be organized
{
myA[temp1]=i+1; // Store into A array starting at index 1
temp1++; // index the parameters to the next index
}
else if(myArray[i]>=80 && myArray[i]<=89) //Use and operator to set parameter
{
myB[temp2]=i+1; //Store in B array
temp2++; // index array
}
else if(myArray[i]>=70 && myArray[i]<=79) //Use and operator to set parameter
{
myC[temp3]=i+1; //Store in C array
temp3++; //
}
else if(myArray[i]>=60 && myArray[i]<=69) //Use and operator to set parameter
{
myD[temp4]=i+1;
temp4++;
}
else if(myArray[i]>=40 && myArray[i]<=59) //Use and operator to set parameter
{
myF[temp5]=i+1;
temp5++;
}*/

}
//Test to see random numbers are indexed correctly(comment out above code to test)
document.write(myArray);

Right now, that document.write works and writes out all my randoms.. when you take out the comment tags though and put in a document.write for lets say myA, it no longer writes anything. Ive checked and double checked to see for any syntax errors as far as i see, theres non.. so why doesnt it write out my arrays when i take the comment tags out??

Klmpee
11-12-2010, 04:11 PM
Okay disregard the last post, i set the temps as variables and made them equal to zero and adjusted the code according

Philip M
11-12-2010, 05:05 PM
So your thread is resolved? If so, you can mark it as such.

<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.