Plus Minus

Sort by

recency

|

282 Discussions

|

  • + 0 comments

    c++20

    void plusMinus(vector<int> arr) {
        size_t nPositive = 0;
        size_t nNegative = 0;
        size_t nZero = 0;
        
        for(int val : arr){
            if(val > 0){
                nPositive += 1;
            }
            else if(val < 0){
                nNegative += 1;
            }
            else{
                nZero += 1;
            }
        }
        
        double fracPositive = nPositive/static_cast<double>(arr.size());
        double fracNegative = nNegative/static_cast<double>(arr.size());
        double fracZero = nZero/static_cast<double>(arr.size());
        
        std::cout << std::fixed;
        std::cout << std::setprecision(6);
        std::cout << fracPositive << std::endl;
        std::cout << fracNegative << std::endl;
        std::cout << fracZero << std::endl;
    }
    
  • + 0 comments
        let positive =0, negative=0, zero = 0
        let finalArr = []
        arr.map((n,i) =>{
            if(n>0) positive++
            if(n<0) negative++
            if(n===0) zero++
        })
        finalArr[0] = (positive / arr.length).toFixed(6)
        finalArr[1] = (negative / arr.length).toFixed(6)
        finalArr[2] = (zero / arr.length).toFixed(6)
        console.log(finalArr.join("\n"))
    
  • + 0 comments

    BI

  • + 0 comments

    C++11

    /* * Complete the 'plusMinus' function below. * * The function accepts INTEGER_ARRAY arr as parameter. */

    void plusMinus(vector arr) { float positive = 0, negative = 0, zero = 0; int total = arr.size();

    for(int i=0; i<total; i++){
        if(arr[i]>0){
            positive++;
        }
        else if(arr[i]<0){
            negative++;
        }
        else{
            zero++;
        }
    }
    cout<< positive/total <<endl;
    cout<< negative/total <<endl;
    cout<< zero/total <<endl;
    

    }

  • + 0 comments

    Scala

    def plusMinus(arr: Array[Int]) {
        // Write your code here
        val n = arr.length
        var positiveCount, negativeCount, zeroCount = 0
        
        arr.map(elem => {
            if(elem > 0) positiveCount = positiveCount + 1
            else if(elem < 0) negativeCount = negativeCount + 1
            else zeroCount = zeroCount + 1
        })
        val positiveRatio = positiveCount.toFloat/n
        val negativeRatio = negativeCount.toFloat/n
        val zeroRatio = zeroCount.toFloat/n
        
            println(f"$positiveRatio%.6f")
            println(f"$negativeRatio%.6f")
            println(f"$zeroRatio%.6f")
        }
    }