0
The goal 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 it is negative I have to count +1 to a counter (that is, add positives 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 the positives nor show the array.
I assume it is the array that 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>
I had not seen that it was to display at each iteration, I corrected my response from a look :)
– Caique Romero