Click here to Skip to main content
15,867,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am stuck on this problem of hackerrank.I wrote this code on vscode and it works fine their. Output is exactly as per requirment but on hackerrank compiler it gives wrong input.
Here is the problem link

Link to problem

STDIN
6
-4 3 -9 0 4 1


STDOUT on vscode
0.5
0.333333
0.166667

STDOUT on Hackerrank compiler
0.333333
0.666667
0


What I have tried:

#include <iostream>
using namespace std;
int main()
{
    int n=0;
    float temp1=0,temp2=0,temp3=0;
    int arr[]={};
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<n;i++)
    {
        if(arr[i]>0)
        {
            temp1++;
        }
        else if(arr[i]<0)
        {
            temp2++;
        }
        else if(arr[i]==0)
            temp3++;
    }
    cout<<temp1/n<<endl;
    cout<<temp2/n<<endl;
    cout<<temp3/n<<endl;
}
Posted
Updated 1-May-22 9:17am
Comments
0x01AA 1-May-22 14:51pm    
And vs does not warn you with '*** stack smashing detected ***'?
The problem is int arr[]={};. Think about what that means ;)
Hamza Jameel 1-May-22 14:52pm    
Yea fixed it by redeclaring array "int arr[n]"
Thanks :)

1 solution

Answered only to remove from unanswered queue: solved by OP (with help from 0x01AA)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900