Where dose the compiler encounter floatinng point int the below program? What is the meaning of core dumped?

The below code is returning floating point error, where does it find the error from?

#include<iostream>

using namespace std;

int main(){
    int x[10];
    int y = 1000000000;
    long z = 2312432321;
    int a = 0;
    for(int i = 0; i < 10; i++){
        a = z/y;
        x[i] = a;
        y /= 10;
        z %= y;
    }

    for(int j = 0; j < 10; j++){
        cout << x[j] << " ";
    }

}

//output Floating point exception (core dumped).

Answer

The error comes at execution time, not at compile time (so it is not the compiler that encounters the error).

On the last iteration, y /= 10 makes y be 0, so z %= y throws an error.

Also, as mentionned in the comments, you should use a debugger to detect those errors, that will help you in many situations, and save you a lot of time.

Leave a Reply

Your email address will not be published. Required fields are marked *