Calculating Ratios of Positive, Negative, and Zero Elements in an Array Using PHP

Efficiently Calculating Ratios of Positive, Negative, and Zero Elements in a PHP Array

Introduction

When working with arrays of integers, we often need to analyze their contents. One common task is to calculate the proportion of positive, negative, and zero values in a given array. This article explores how to achieve this in PHP and provides a detailed breakdown of the logic and implementation.

Calculating Ratios of Positive, Negative, and Zero Elements in an Array Using PHP
Calculating Ratios of Positive, Negative, and Zero Elements in an Array Using PHP

Problem Statement

Given an array of integers, we need to compute three ratios:

  1. The proportion of positive numbers.
  2. The proportion of negative numbers.
  3. The proportion of zeros.

Each ratio should be printed as a decimal value rounded to six decimal places.

Approach to Solve the Problem

To solve this problem efficiently, we follow these steps:

  1. Read the array of numbers — The user provides a series of numbers as input.
  2. Count the occurrences of positive, negative, and zero elements.
  3. Compute the ratios by dividing each count by the total number of elements.
  4. Print the results with six decimal places for accuracy.

PHP Implementation

The following PHP script reads an array of integers from user input, processes the data, and prints the required ratios.

<?php
function plusMinus($arr) {
$n = count($arr);
$pos = 0;
$neg = 0;
$zero = 0;

// Count positive, negative, and zero elements
foreach ($arr as $num) {
if ($num > 0) {
$pos++;
} elseif ($num < 0) {
$neg++;
} else {
$zero++;
}
}

// Calculate and print ratios with six decimal places
printf("%.6f\n", $pos / $n);
printf("%.6f\n", $neg / $n);
printf("%.6f\n", $zero / $n);
}

// Taking input from user
echo "Enter numbers separated by space: ";
$handle = fopen("php://stdin", "r");
$input = trim(fgets($handle));
$arr = explode(" ", $input);
$arr = array_map('intval', $arr); // Convert input strings to integers

plusMinus($arr);
?>

Code Explanation

1. Function plusMinus($arr)

This function takes an array of integers and performs the following operations:

  • It initializes three counters: $pos for positive numbers, $neg for negative numbers, and $zero for zeroes.
  • It loops through the array and updates the respective counters.
  • Finally, it calculates the required ratios and prints them using printf("%.6f\n", value); to ensure six decimal places.

2. Taking User Input

  • fgets($handle) reads user input as a single string.
  • explode(" ", $input) splits the string into an array of individual numbers.
  • array_map('intval', $arr) converts these string values into integers.

Example Run

Input:

1 1 0 -1 -1

Processing:

  • Positive numbers: 1, 12 elements
  • Negative numbers: -1, -12 elements
  • Zeros: 01 element
  • Total elements = 5

Computed Ratios:

Calculating Ratios of Positive, Negative, and Zero Elements in an Array Using PHP
Calculating Ratios of Positive, Negative, and Zero Elements in an Array Using PHP

Output:

0.400000
0.400000
0.200000

Time Complexity Analysis

  • Looping through the array takes O(n) time.
  • Counting and calculating ratios take O(1) time.
  • Overall complexity: O(n), where n is the number of elements in the array.

This makes our approach efficient for large input sizes.

Conclusion

In this article, we explored how to calculate the proportion of positive, negative, and zero values in an array using PHP. We used a simple loop to count occurrences and formatted the output to six decimal places for precision. This approach is efficient and works well for both small and large datasets.

Taran
Taran

Written by Taran

Writing daily on Medium about programming, problem-solving, and multiple languages. Aspiring full-time writer.

No responses yet

Write a response