Monday, April 11, 2016

Plus Minus

Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and 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 104 are acceptable.
Input Format
The first line contains an integer, N, denoting the size of the array.
The second line contains N space-separated integers describing an array of numbers (a0,a1,a2,,an1).
Output Format
You must print the following 3 lines:
  1. A decimal representing of the fraction of positive numbers in the array.
  2. A decimal representing of the fraction of negative numbers in the array.
  3. 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
Explanation
There are 3 positive numbers, 2 negative numbers, and 1 zero in the array.
The respective fractions of positive numbers, negative numbers and zeroes are 36=0.50000026=0.333333 and 16=0.166667, respectively.

SOLUTION:


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

int main(){
    int n;
    double p=0,ne=0,z=0;             //p for positive,ne for negative,z for zero valued 
    scanf("%d",&n);
    int arr[n];
    for(int arr_i = 0; arr_i < n; arr_i++){
       scanf("%d",&arr[arr_i]);
        (arr[arr_i]==0)?z++:((arr[arr_i])>0?p++:ne++);
       
    }
    printf("%f\n%f\n%f",(p/n),(ne/n),(z/n));
    return 0;
}