FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Array problem

 
Post new topic   Reply to topic    Cambodia eXPlore Forum Index -> Flash Shockwave
Author Message
guitarman
fourth grade


Joined: 04 Nov 2005
Posts: 726

PostPosted: Sun Nov 06, 2005 12:48 am    Post subject: Array problem Reply with quote

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


Cheers Smile

Back to top
Display posts from previous:   
Post new topic   Reply to topic    Cambodia eXPlore Forum Index -> Flash Shockwave All times are GMT + 8 Hours
Page 1 of 1



by phpBB