Tuesday, October 21, 2008

Finding the Smallest or Largest Number in an Unordered Series

// define number series
$series = array(76, 7348, 56, 2.6, 189, 67.59, 17594, 2648, 1929.79,↵
54, 329, 820, -1.10, -1.101);

// sort array
sort($series);
// extract maximum/minimum value from sorted array
// result: "Minimum is -1.101 "
$min = $series[0];
echo "Minimum is $min ";

// result: "Maximum is 17594"
$max = $series[sizeof($series)-1];
echo "Maximum is $max";

Rounding a Floating Point Number

// define floating point number
$num = (2/3);

// round to integer
// result: 1
$roundNum = round($num);
echo $roundNum . "\n";

// round to 1 decimal place
// result: 0.7
$roundNum = round($num, 1);
echo $roundNum . "\n";

// round to 3 decimal places
// result: 0.667
$roundNum = round($num, 3);
echo $roundNum;

// define floating point numbers
$num = (1/3);
$r = round($num);
$c = ceil($num);
$f = floor($num);
// result: "0 1 0"
echo "$r $c $f"

Generate a number List /Range

//define range limits
$x = 10;
$y = 36;
// generate range as array
// result: (10, 11, 12...35, 36)
$range = range($x, $y); print_r($range);

// define range limits
$x = 10;
$y = 30;
// generate range as array
// contains every third number
// result: (10, 13, 16, 19, 22, 25, 28)
$range = range($x, $y, 3);
print_r($range);

// print multiplication table
foreach (range(1, 10) as $num) {
echo "5 x $num = " . (5 * $num) . "\n";
}