Sunday, 16 August 2015

Plus Minus Hacker Rank Problem Solution Using C++.

/*
Problem Statement
You're given an array containing integer values. You need to print the fraction of count of positive numbers, negative numbers and zeroes to the total numbers. Print the value of the fractions correct to 3 decimal places.
Input Format
First line contains N, which is the size of the array.
Next line contains N integers A1,A2,A3,,AN, separated by space.
Constraints
1N100
100Ai100
Output Format
Output three values on different lines equal to the fraction of count of positive numbers, negative numbers and zeroes to the total numbers respectively correct to 3 decimal places.
Sample Input
6
-4 3 -9 0 4 1          
Sample Output
0.500
0.333
0.167
Explanation
There are 3 positive numbers, 2 negative numbers and 1 zero in the array.
Fraction of the positive numbers, negative numbers and zeroes are 36=0.50026=0.333and 16=0.167 respectively.
Note This challenge introduces precision problems. You can even print output to 4 decimal places and above but only the difference at 3rd decimal digit is noted. That is the reason you'll notice testcases have much higher precision (more decimal places) than required.
Answers with absolute error upto 104 will be accepted.
TAG: C++,Array Implementation , Hacker Rank,Precision Problems.
*/

Solution:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    float n,cnt = 0,a,p = 0,ne = 0,z = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a;
        if(a > 0 )p++;
        else if(a < 0)ne++;
        else if(a == 0)z++; 
    }
    printf("%.4f\n",p/n);
    printf("%.4f\n",ne/n);
    printf("%.4f\n",z/n);

    return 0;
}

3 comments:

  1. can u please tell me error in this..


    int main(){
    int n;
    float fract1=0,fract2=0,fract3=0;
    int 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]);
    }
    for(int arr_i = 0; arr_i < n; arr_i++){
    if(arr[arr_i]>0) pos++;
    else if(arr[arr_i]<0) neg++;
    else if(arr[arr_i]==0) zer++;
    }
    fract1=pos/n;
    fract2=neg/n;
    fract3=zer/n;
    printf("%.4f\n",fract1);
    printf("%.4f\n",fract2);
    printf("%.4f\n",fract3);
    return 0;
    }

    ReplyDelete
  2. just declare
    int pos=0,neg=0,zer=0
    as
    float pos=0,neg=0,zer=0;

    ReplyDelete
  3. #include
    int main()
    {
    int sneg=0,spos=0,szero=0;
    float frac1,frac2,frac3;
    int i,n,a[100];
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
    if(a[i]<0)
    sneg=sneg+1;
    else if(a[i]==0)
    szero=szero+1;
    else
    spos=spos+1;


    }
    frac1=sneg/n;
    frac2=spos/n;
    frac3=szero/n;
    printf("%.6f/n",frac1);
    printf("%.6f/n",frac2);
    printf("%.6f/n",frac3);

    return 0;
    }sir what is the error in this code

    ReplyDelete