Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.

Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.

Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with  places after the decimal.


// Complete the plusMinus function below.
function plusMinus($arr) {

        $pos=0;
        $neg=0;
        $zero=0;
    for($i=0;$i<count($arr);$i++)
    {
        if($arr[$i]>0)
        {
            $pos++;
        }
        else if($arr[$i]< 0)
        {
            $neg++;
        }
        else
        {
            $zero++;
        }
    }
   $pos= $pos/count($arr);
 $neg= $neg/count($arr);
 $zero= $zero/count($arr);
$res= array($pos,$neg,$zero);
    echo $res[0]."\n".$res[1]."\n".$res[2];

}

$stdin = fopen("php://stdin", "r");

fscanf($stdin, "%d\n", $n);

fscanf($stdin, "%[^\n]", $arr_temp);

$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));

plusMinus($arr);

fclose($stdin);



  
    

0 Response to "Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel