forked from milq/milq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.php
127 lines (90 loc) · 2.67 KB
/
05.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php // FUNCTIONS
/**
* Prints 'Hello, world!'
*/
function hello()
{
echo '<p>Hello, world!</p>';
}
/**
* Returns the square of a number
* @param {Number} x - Number to be squared.
* @returns {Number} y - Square of x
*/
function square($x)
{
$y = $x * $x;
return $y;
}
/**
* Returns the sum of a and b
* @param {Number} a - First number.
* @param {Number} b - Second number.
* @returns {Number} sum - Sum of a and b
*/
function sum($a, $b)
{
$sum = $a + $b;
return $sum;
}
/**
* Returns the product of a and b
* @param {Number} a
* @param {Number} b
* @param {Boolean} retArr - If set to true, the function will return an array
* @returns {Number|Array} - Product of a and b or an array that contains a, b and the product of a and b.
*/
function product($a, $b, $retArr) {
if ($retArr) {
return [$a, $b, $a * $b];
}
return $a * $b;
}
/**
* Returns the mean of an array of numbers, the square of a number, and an array of strings sorted in alphabetical order.
* @param {Array} nmbs - Array of numbers
* @param {Number} n - Number to be squared
* @param {Array} strs - Array of strings
* @returns {Array} result - Array that contains the mean of 'nmbs', the square of 'n', and an array of ordered strings
*/
function mean_square_sort($nmbs, $n, $strs)
{
$aux = 0;
for ($i = 0; $i < count($nmbs); $i++) {
$aux = $aux + $nmbs[$i];
}
$mean = $aux / count($nmbs);
$squared = square($n);
$array_sorted = $strs; // Clone
sort($array_sorted); // Sort
$result = [$mean, $squared, $array_sorted];
return $result;
}
/**
* Returns the factorial of a number (the number must be integer) using recursion
* @param {Number} x - Number that is an integer.
* @returns {Array} res - The factorial of a number
*/
function factorial($x) {
if ($x <= 1) {
return 1;
}
$res = $x * factorial($x - 1); // factorial is being called again
return $res;
}
// CALLING FUNCTIONS
hello();
$x = 2;
$sqr = square($x);
echo '<p>The square of ' . $x . ' is ' . $sqr . '.</p>';
$s = sum(square(2), 3);
echo '<p>The sum of ' . square(2) . ' and ' . 3 . ' is ' . $s . '.</p>';
$p = product(4, sum(2, square(1)), true);
echo '<p>The product of ' . $p[0] . ' and ' . $p[1] . ' is ' . $p[2] . '.</p>';
$latitudes = [-2.4, 7.4, 3, 4.6, -5];
$number = sum(1, 1);
$weather = ['sunny', 'cloudy', 'windy', 'rainy'];
$res = mean_square_sort($latitudes, $number, $weather);
echo '<p>Return of the \'mean_square_sort\' function: ' . $res[0] . ' ' . $res[1] . ' ' . implode(',', $res[2]) . '.</p>';
echo '<p>The factorial of ' . $res[1] . ' is ' . factorial($res[1]) . '.</p>';
?>