Posted: Sun Nov 06, 2005 12:48 am Post subject: Array problem
Quote:
I have an array consist of 100 elements and each element has a value either 0 or 10. I would like to display the TOTAL of 6 consecutive elements. i.e.
element number : 1 2 3 4 5 6
element value: 0 10 10 0 0 10 > would display 30
element number : 1 2 3 4 5 6
element value: 10 10 10 10 10 10 > would display 60
The script will need to display each possible combination of 6 consecutive elements i.e.
element number : 1 2 3 4 5 6 then;
element number : 2 3 4 5 6 7 then;
element number : 3 4 5 6 7 8 e.t.c
Okay, here is the actionscript my way. To test, you just need to copy and paste into your new .fla on first frame. and press Ctrl+Enter for testing:
//initialiazation an array and populate it with 100 elements with value of either 0 or 10;
myArray=new Array();
for (var i=0;i<100;i++) {
myNum= Math.random();
myNum=Math.round(myNum)*10;
myArray.push(myNum);
}
trace("The random Array value between 0 and 10 are : "+ myArray);
sum=0; //initialiazation value of sum to Zero prior to addition
for (j=0;j<95;j++){
for (k=j;k<j+6;k++){ // when maximum value of k could reach is 99
trace(k); //view the 6 consecutively value
sum= sum+myArray[k]; // add the 6 consecutively value and save to sum
}
trace("sum is "+sum); // trace value of sum
sum=0; // initialize sum back to zero for next round of addition.
}