## Problem

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.

Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to are acceptable.

**Function Description**

Complete the plusMinus function in the editor below.

plusMinus has the following parameter(s):

```
int arr[n]: an array of integers
```

**Print**

Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with digits after the decimal. The function should not return a value.

**Input Format**

The first line contains an integer, n, the size of the array.

The second line contains n space-separated integers that describe arr[n].

**Output Format**

Print the following lines, each to decimals:

- proportion of positive values
- proportion of negative values
- proportion of zeros

## Solution (How i solved it)

- Take three variables named "positive", "negative" and "neutral. Store initial value as zero(0).
- Iterate a for loop through the given array.
- While iterating the array we have to add to either the positive, negative and neutral values.
- Calculate the ratios by dividing with the array length.
- Print the results. Here is a tricky part, The instruction says, print each result on new line, if you are using echo or print function, it wont go to new line, so my solution is to use PHP_EOF.

```
$postive = 0;
$negative = 0;
$neutral = 0;
$arrsize = sizeof($arr);
for ($i = 0; $i < $arrsize; $i++){
if ($arr[$i] > 0){
$positive++;
}else if ($arr[$i] < 0){
$negative++;
}else if ($arr[$i] === 0){
$neutral++;
}
}
$pos_res = number_format($positive / $arrsize, $arrsize);
$neg_res = number_format($negative/ $arrsize, $arrsize);
$neu_res = number_format($neutral / $arrsize, $arrsize);
echo $pos_res . PHP_EOL;
echo $neg_res . PHP_EOL;
echo $neu_res. PHP_EOL;
```

Link to the HackerRank: Plus Minus Test

## Top comments (0)