We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Modal Average
Topic Started: Mar 13 2005, 01:49 PM (270 Views)
mathiaus
Member Avatar
mathiaus maximus™
[ *  *  *  *  *  * ]
Just wondering if there was a way using either php or javascipt (preferably php but anything will do!) of working out the modal average of a bunch of numbers?

I've checked google and some sites like phpfreaks but returned with no results thus far which would lead me to believe there is no way, however I just thought I'd ask you lot before I gave up hope.

Thanks and Salutations
MarthaStewart
( :yoda: joke!)
Offline Profile Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
should be able to,

how are these numbers defined? in an array?
Offline Profile Quote Post Goto Top
 
mathiaus
Member Avatar
mathiaus maximus™
[ *  *  *  *  *  * ]
It's sort of like a quiz with checkboxes. Each question has 4 answers each with a different number of points assigned to each one.

eg.
Q1
1
2
3
4

Q2
1
2
3
4

so say the person chose 1 and then 1 I'd like if it's possible to come up 1
(lots more questions obviously!)
Offline Profile Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
in Js u could have a function to add one to the value of four different variables, dependant on a form input. then you could make a simple logic routine at the end to figure out which of the four variables ends up with the highest value.
Offline Profile Quote Post Goto Top
 
mathiaus
Member Avatar
mathiaus maximus™
[ *  *  *  *  *  * ]
Thanks! I'll go and try that out then...
Offline Profile Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
if you need any help i can make you a little template one...i was thinking u could do the same in php using the same method
Offline Profile Quote Post Goto Top
 
mathiaus
Member Avatar
mathiaus maximus™
[ *  *  *  *  *  * ]
Right then I've got this in php

Code:
 
<?php

$data = array( 1,2,2,5,6,3,2,1,2,6,7,7);

# mode - number occuring most often
$modearray = array_count_values($data);
arsort($modearray);
list ($mode, $count) = each($modearray);

echo $mode;

?>


That works fine etc and is shorter than what the javacript would be I think?

I started on the javascript but my knoledge of it is very small (size of a pea maybe another 1/2 if I'm lucky!).
Offline Profile Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
cool...yeah the js would have been larger, i though it was for an IF site @ first so suggested js.lol
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply