Jump to content

[SOLVED] Finding the mode???


sgalatas

Recommended Posts

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!  ???

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

 

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'];

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.