-3

while solving a question on hackerearth i got this error , i tried to find solution but everywhere this error occurs when 2 different datatypes are used as operands but in my case both the operands of sama data type, here is my full code

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int arr[1000];
    int n;
    cin>>n;
    double ans=1;
    for(int i =0; i<n ;i++)
    {
        cin >> arr[i];
        ans=(ans*arr[i])%(pow(10,9)+7);
    }
    cout << ans;
    return 0;

}
zoyron
  • 3
  • 1
  • 4

2 Answers2

1

What's the expected output of 12.3 % 4.2 ? If you have an answer for that, you also have to roll your own implementation, built in operator% works for integer types only.

If you are fine with that, but your arguments happen to be floating point values, round/floow/ceil/cast them. e.g:

static_cast<uint64_t>(double_value);
erenon
  • 18,838
  • 2
  • 61
  • 93
0

Module operator (i.e. operator%) is not defined for floating points, but for integers only.

As you declared ans as a double, your expression ans*arr[i] evaluates as a double. Changing it to int ans will have the erroneous statement to compile (actually returning the integer modulo).

Ad N
  • 7,930
  • 6
  • 36
  • 80