Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 11 of 11
Thread: array

10182008, 02:27 PM #1
 Join Date
 Oct 2008
 Posts
 8
 Thanks
 2
 Thanked 0 Times in 0 Posts
array
resolved
Last edited by flouriet; 10202008 at 11:31 AM. Reason: resolved
10182008, 03:40 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,907
 Thanks
 203
 Thanked 2,531 Times in 2,509 Posts
Sounds like homework! Have a look at the posting guidlines.
Show us your effort, and then perhaps someone will help you improve it.
Quizmaster. Who was the Roman god of war? I'll give you a clue. It is the name of a type of chocolate bar.
Contestant: Twix.
10192008, 05:58 PM
#3
 Join Date
 Oct 2008
 Posts
 8
 Thanks
 2
 Thanked 0 Times in 0 Posts
I get very confused with this stuff, is there any recommended books that I can read to help me. thhanks
10192008, 08:02 PM
#4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,907
 Thanks
 203
 Thanked 2,531 Times in 2,509 Posts
Your work has quite a lot of errors. I have worked through some of them for you but I have run out of time and don't think I ought to do all your homework for you, so the standard devaition calculation still needs correction. You should also check manually that the various calculations are in fact correct. I trust you will learn from this rather than blindly copy it.
Here is a script to calculate the standard devaition:Code:<html> <script type="text/javascript"> function divide_string() { var data=prompt("Enter your data (separate with a space)",""); data = data.replace(/[^\d\s]/g,"") // strip nonnumeric data = data.replace(/\s+/g, " "); //strip double spaces new_array = data.split(" "); // spliting the new array new_array = new_array.sort(function(a,b){return a  b}) var len= new_array.length; //length of array or number of elements var halfLen = len/2; // half length of the number of elements var med = Math.floor(halfLen);// find the median if(med == halfLen) { median = (new_array[med+1]  new_array[med])/2; } else { median = new_array[med]; } var max=new_array[len1]; var min=new_array[0]; var sum = 0; var stddevn = 0; for (i=0; i<len; i++) { sum = sum + (parseFloat(new_array[i])); // sum } var a= "";// each data squared then the sum of it var b= (sum*sum)/len; // part two of stddevn ( means sum of the data squared divided by number of elements var mean = (sum/len); // mean stddevn = Math.sqrt((ab)/(len1)); // standard deviation alert ("Array: "+new_array+", The mean is: " + mean + " The standard deviation is: " + stddevn+ " The median is: " + median + " The maximum is: " + max +" The min is: " + min) } </script> <FORM> <INPUT TYPE="button" onClick="divide_string()" value="Click here to enter your data"> </FORM> </html>
Sign on a Maine shop: Our motto is to give our customers the lowest possible prices and workmanship.Code:<script type = "text/javascript"> var data = new Array("73", "58", "67", "93", "33", "18", "147"); var deviation = new Array(); var sum = 0; var devnsum = 0; var stddevn = 0; var len = data.length; for (var i=0; i<len; i++) { sum = sum + (data[i] * 1) // ensure number } var mean = (sum/len).toFixed(6); // 6 decimal places for (i=0; i<len; i++) { deviation[i] = data[i]  mean; deviation[i] = deviation[i] * deviation[i]; devnsum = devnsum + deviation[i]; } stddevn = Math.sqrt(devnsum/(len1)).toFixed(6); // 6 decimal places alert ("The mean is: " + mean + " The standard deviation is: " + stddevn) </script>
Last edited by Philip M; 10192008 at 08:08 PM.
Users who have thanked Philip M for this post:
forthfriend (10202008)
10192008, 08:42 PM
#5
 Join Date
 Oct 2008
 Posts
 8
 Thanks
 2
 Thanked 0 Times in 0 Posts
thanks alot
could you tell me why you used a,b and used this function
new_array.sort(function(a,b){return a  b})
thanks for helping me
i realize that some of the equations were wrong but I was able to fix them
thanks once again
Last edited by flouriet; 10202008 at 11:45 AM.
10192008, 10:08 PM
#6
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,907
 Thanks
 203
 Thanked 2,531 Times in 2,509 Posts
There is a good explanation at
http://www.javascriptkit.com/javatutors/arraysort.shtml
10192008, 11:54 PM
#7
 Join Date
 Oct 2008
 Posts
 8
 Thanks
 2
 Thanked 0 Times in 0 Posts
somehow this doesn't work
Last edited by flouriet; 10202008 at 12:52 PM.
10202008, 08:05 AM
#8
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,907
 Thanks
 203
 Thanked 2,531 Times in 2,509 Posts
Sorry, I can't take over the role of your teacher.
"I have answered three questions, and that is enough,"Code:var a=[1,2,3,4,5,6]; var len=a.length; var med=len/2; if (len%2==0) median=(a[med]+a[med1])/2; else median=a[Math.floor(med)]; alert(median); // RESULT 3.5
Said his father. "Don't give yourself airs!
Do you think I can listen all day to such stuff?
Be off, or I'll kick you down stairs."
 Lewis Carroll
10202008, 08:43 AM
#9
 Join Date
 Oct 2008
 Posts
 8
 Thanks
 2
 Thanked 0 Times in 0 Posts
i really appreciate your help but when i did the first part i got the same answer as you but when i put it into my code and typed in teh same series of numbers it gave me the median of 22.5
10202008, 11:50 AM
#10
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,907
 Thanks
 203
 Thanked 2,531 Times in 2,509 Posts
Use alerts after each line to check the progress of the calculation. And be careful to ensure that your variables are numbers, not strings.
Users who have thanked Philip M for this post:
flouriet (10202008)
10202008, 12:59 PM
#11
 Join Date
 Oct 2008
 Posts
 8
 Thanks
 2
 Thanked 0 Times in 0 Posts
thanks for all of your help
I didn't realize you're from London oh I was there about a year ago and love London.