The purpose of the exercise was to create an array of 20 numbers ranging from -100 to 100. I have to distinguish between negative and positive values. If it is positive I have to add the value in its entirety to a variable and if they are negative I have to count +1 to a counter (ie add positive and count the number of negatives).
For some reason I am able to count the negatives but I am not able to calculate the sum of positives or show the array.
I assume the array is not being saved correctly. My code is as follows:
<html>
<head>
<meta charset="UTF-8">
<title>PHP Test</title>
</head>
<body>
<?php
function random(){
$vec = array();
$neg = 0;
$pos = 0;
for ($i=1; $i<=20; $i++) {
$temp = rand(-100, 100);
$vec = $temp;
if ($temp <= 0) {
$neg++;
} else {
$pos+=$vec;
}
}
echo "Soma dos Positivos </br>" . $sum . "Numero de negativos " . $neg . "</br>";
for ($i=1; $i<=20; $i++) {
return $vec[$i];
}
}
random();
?>
</body>
</html>