SHARE
TWEET

Untitled

a guest Nov 12th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Represents an array of positive numbers
  4.  *
  5.  */
  6. class PositiveNumericArray {
  7.  
  8.  
  9.     //Add a private member variable here
  10.     private $inputs;
  11.     private $sum;
  12.     /*
  13.      * Creates an instance of the PositiveNumericArray class
  14.      *
  15.      * @param  array of integers
  16.      */
  17.     public function __construct(array $inputs) {
  18.     $this->inputs = $inputs;
  19.     }
  20.  
  21.     /**
  22.      * getSum() returns the numeric sum of the input array elements
  23.      * example the input array(3,2,1) should return 6 (3+2+1)
  24.      *
  25.      * @return integer total numeric content of the array
  26.      */
  27.     function getSum() : int {
  28.         if($this->inputs != null){
  29.         foreach ($this->inputs as $key => $value) {
  30.         $this->sum = $this->sum + $value;
  31.         }
  32.        
  33.         return $this->sum; //add code here to calculate the sum
  34.         }else{
  35.         return 0;
  36.         }
  37.     }
  38.  
  39.     /**
  40.      * getSmallest() returns the lowest numeric item of the input array elements
  41.      * example the input array(3,2,1) should return 1
  42.      *
  43.      * Note that for empty arrays the method is allowed to return any integer,
  44.      * this will be solved in the next assignment
  45.      *
  46.      * @return integer lowest item of the array
  47.      */
  48.     function getSmallest() : int {
  49.  
  50.         if(count($this->inputs) > 0){
  51.         $temp = 0;
  52.         for($i = 0; $i < count($this->inputs); $i++){
  53.             for($y = $i+1; $y < count($this->inputs); $y++){
  54.            
  55.                 if($this->inputs[$i] > $this->inputs[$y]){
  56.                     $this->temp = $this->inputs[$i];
  57.                     $this->inputs[$i] = $this->inputs[$y];
  58.                     $this->inputs[$y] = $this->temp;
  59.                 }
  60.         }
  61.         }
  62.         return $this->inputs[0];
  63.        
  64.         }else{
  65.         return 0;
  66.         }
  67.        
  68.     }      
  69.  
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top