Plus Minus Hackerrank Problem coderinme

Plus Minus

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 iPlus Minusnteger, , denoting the size of the array.
The second line contains space-separated integers describing an array of numbers .

Output Format

You must print the following lines:

A decimal representing of the fraction of positive numbers in the array.
A decimal representing of the fraction of negative numbers in the array.
A decimal representing of the fraction of zeroes in the array.
Sample Input

6
-4 3 -9 0 4 1
Sample Output

0.500000
0.333333
0.166667

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int i, n,a[100],pos=0, neg=0,zer=0;
    float x;
    scanf("%d",&n);
    for(i=0; i<n; i++){
        scanf("%d",&a[i]);        
    }
    for(i=0; i<n; i++){
       if(a[i]==0)
           ++zer;
       else if(a[i]>0)
           ++pos;
           else 
           ++neg;
      
    }
    x=pos/(float)n;
    printf("%.3f",x);
    x=neg/(float)n;
     printf("\n%.3f",x);
    x=zer/(float)n;
     printf("\n%.3f",x);

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Plus Minus
Competitive coding
Hackerrank problem

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.