Calculate the fractions of its elements that are positive, negative, and are zeros.

5. Given an array of integers, calculate the fractions of its elements that are positivenegative, and are zeros. Print the decimal value of each fraction on a new line.
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 .
Output Format             
You must print the following 3 lines:
1.       A decimal representing of the fraction of positive numbers in the array compared to its size.
2.       A decimal representing of the fraction of negative numbers in the array compared to its size.
3.       A decimal representing of the fraction of zeros in the array compared to its size.
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 proportions of occurrence are positive: 3/6=0.500000 , negative: 2/6=0.333333 and zeros: 1/6=0.166667.

SOLUTION IN C

#include <stdio.h>
int main()
{
    int a[50],n,i,cp=0,cn=0,cz=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {               
        if(a[i]>0)
            cp++;
        else if(a[i]<0)
            cn++;
        else
            cz++;
    }
    printf("%f",(float)cp/n);
    printf("\n%f",(float)cn/n);
    printf("\n%f",(float)cz/n);
}

No comments:

Post a Comment

Total Pageviews