0
void plusMinus(vector<int> arr) {
    double p=0, n=0, z=0;
    for(int i=0; i<arr.size(); i++)
        if(arr[i]>0)
            p += 1/arr.size();
        else if(arr[i]<0)
            n += 1/arr.size();
        else
            z += 1/arr.size();
    cout<<p<<endl<<n<<endl<<z;
}

The values of p, n, and z are not changing in the for loop. Can anyone tell me why?

cigien
  • 57,834
  • 11
  • 73
  • 112

1 Answers1

1

You do 1/arr.size() in the function. The result is an integer with the value 0.

You need to use at least one double in the division.

Example:

p += 1. / arr.size();

Do the same for n and z too.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108