sgalatas Posted November 12, 2007 Share Posted November 12, 2007 Good Afternoon, Does anyone know how to write a program to find the mode of a series of numbers? I need to find the mean, median and mode. I was able to find the formula's for the mean and the median. But I have not been able to find a formula to find the mode. I know that the mode will be the numbers that are listed multiple times. I just can not figure out how to write this in the program to echo the mode. HELP! ??? Quote Link to comment Share on other sites More sharing options...
severndigital Posted November 12, 2007 Share Posted November 12, 2007 it will be easier to help if we can see the code that you already have. finding the mode of an array is easy, but integrating it into your existing code will be hard if we can't see it. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 12, 2007 Share Posted November 12, 2007 okay so lets assume you have an array of numbers, we will use this array to find the data on it start with array_count then using that find the max value and the key of the max is what you want. Quote Link to comment Share on other sites More sharing options...
sgalatas Posted November 12, 2007 Author Share Posted November 12, 2007 I'm kind of all over the place with this. Here is the code that I have so far. <html> <head> </head> <body> <h1 style="text-align: center; color: navy" >Sports Stats</h1> <form action="sportspage.php" method="get"> <fieldset> <label for="stat">Enter Stats</label> <input type= "text" name= "stat" id="stat" size="5" /> </fieldset> <!----> <fieldset> <input type= "submit" name= "submit" id="submit" value= "Submit" /> <input type="reset" name="reset" value="Cancel" /> </fieldset> </form> <?php /** * Author: Sherrie Galatas * Date: November 15, 2007 * * Determine the Mean, Mode, and Median of the Array */ $stat = ""; $statArray = array($stat); //loop counter $x = 1; $ct= 1; $max= 5; for ($i=0; $i <= 5; $i++); if (is_numeric($_GET['stat']) { $score = $_GET['stat']; $stat = $statArray($stat); foreach ($stat as $key => $value) { echo "Stat". ($key + 1). " = $value<br />"; } } sort($statArray); $mean= array_sum($stat) / count($stat); $median= $statArray[2]; $mode= $ct= count($statArray); $tally = array_count_values($statArray); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 12, 2007 Share Posted November 12, 2007 Use the [c ode] brackets, but try <?php function mode($data){ $temp = array_count_values($data); asort($temp); $mode = array(); $mode['count'] = current($temp); $mode['value'] = ket($temp); return $mode; } ?> Then you can get an array with the greatest count and the greatest value by saying $mode = mode($array); $mode_value = $mode['value']; $mode_count = $mode['count']; Quote Link to comment Share on other sites More sharing options...
sgalatas Posted November 12, 2007 Author Share Posted November 12, 2007 Thanks. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 12, 2007 Share Posted November 12, 2007 so I take it it worked? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.