Plus Minus HackerRank Solution In C-language | | Hackerrank Solutions

Plus Minus Hacker Rank Solution In C-language | | Hacker rank Solutions 

Question

Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and which fraction of its elements are zeroes, respectively. Print the decimal value of each fraction on a new line.
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.
Input Format
The first line contains an integer, , denoting the size of the array. 
The second line contains  space-separated integers describing an array of numbers 


Answer



#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

void plusMinus(int arr_size, int* arr) {
    // Complete this function
}

int main() {
     int n;
    float pos=0,neg=0,zer=0;
    scanf("%d",&n);
    int arr[n];
    for(int arr_i = 0; arr_i < n; arr_i++){
       scanf("%d",&arr[arr_i]);
        if(arr[arr_i]>0)
            pos++;
           else if(arr[arr_i]<0)
               neg++;
               else
               zer++;
    }
    printf("%.5f\n%.5f\n%.5f",(float)pos/n,(float)neg/n,(float)zer/n);
    return 0;
}

Comments :

Post a Comment