0

While executing the following code with a large amount of data (roughly 2MB), I am receiving a Floating Point Exception on line 21.

    #include <bits/stdc++.h>
using namespace std;


int main() {
    std::ios_base::sync_with_stdio(false);
    int S, Q, type, seq, pos;
    int x, y, lastAnswer = 0;
    cin >> S >> Q;
    vector< vector <int> > seqList(S);

    for(int i=0; i<Q; i++){
        cin >> type >> x >> y;
        if( type == 1 ){
            seq = (x^lastAnswer) % S;
            seqList[seq].push_back(y);
            if(seq == 0) lastAnswer = 0;
        }
        else if( type == 2 ){
            seq = (x^lastAnswer) % S;
            pos = (int)(y % seqList[seq].size()); //Line 21
            lastAnswer = seqList[seq][pos];
            cout << lastAnswer << endl;
        }
    }

    return 0;
}
Paul S
  • 25
  • 6
  • 6
    that's because size is 0. And [don't #include ](https://stackoverflow.com/q/31816095/995714) and [don't use `using namespace std`](https://stackoverflow.com/q/1452721/995714) – phuclv Sep 14 '17 at 14:04
  • Which compiler (and version) are you using? Naming *"floating point"* an exception rised by an operation which doesn't involve any floating point type is really misleading. – Bob__ Sep 14 '17 at 14:08
  • You do know that the `^` operator is the bitwise [exclusive or](https://en.wikipedia.org/wiki/Exclusive_or) operator? – Some programmer dude Sep 14 '17 at 14:09

1 Answers1

0
pos = (int)(y % seqList[seq].size()); //Line 21

seqList[seq].size() is 0. @Lưu Vĩnh Phúc

With a%b, if b == 0 then the behavior is not defined. Many platforms report a floating point exception when an integer division or % by zero is attempted.

Re-write to to detect when seqList[seq].size() == 0.

Also check S == 0 as it is used with (x^lastAnswer) % S @Jeremy Friesner

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256