Re: Middle Number

From: Date: Sun, 08 Dec 2002 02:03:23 +0000
Subject: Re: Middle Number
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-127409@lists.php.net to get a copy of this message
At 07:05 PM 12/7/02 -0500, Stephen wrote:
Wouldn't this only work for an even ammount of numbers? Like 1, 2, 3, 4 has 4 numbers...
Did you try it? I'd hate to think I've done more work to solve your problem than you have... Even: $List = array( 1,2,3,4,5,6 ); $Middle = ( count( $List ) - 1 ) / 2;
 2.5     =        ( 6 - 1 ) / 2
$median = ( $List[ floor( $Middle ) ] + $List[ ceil( $Middle ) ] ) / 2;
           ( $List[ 2 ]                + $List[ 3 ]               ) / 2
  3.5    =          3                  +        4                 /2
Odd: $List = array( 1,2,3,4,5 ); $Middle = ( count( $List ) - 1 ) / 2;
 2     =        ( 5 - 1 ) / 2
$median = ( $List[ floor( $Middle ) ] + $List[ ceil( $Middle ) ] ) / 2;
           (  $List[ 2 ]                + $List[ 2 ]              ) / 2
  3      = (        3                  +        3                 ) / 2
It looks like it works to me. For details see: http://mathforum.org/library/drmath/view/57598.html
At 09:45 PM 12/6/02 -0500, Stephen wrote:
How can you find the meadian (or middle number) of a list of numbers? If there is an even amount of numbers, how would I still find it?
load the list into an array, in numeric order... then: $List = array( 1,2,3,4,5,6 ); $Middle = ( count( $List ) - 1 ) / 2; $median = ( $List[ floor( $Middle ) ] + $List[ ceil( $Middle ) ] ) / 2; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#127409) next »