Floating Point Exception( Core Dump

Floating Point Exception( Core Dump

Program: So I made a program that take two numbers, N and L. N is the size of a 2D array and L is a number from 3 - 16. The program builds the array and starts at the center and works its way out in a counter clockwise spiral. I is the value of the center and its as you go through the array( in the spiral ) the value will increase by one. It it is prime, that number will be assigned to that spot and if not it * will take its place instead.

Error: I'm getting a "Floating point exception " error, how would I solve this?

Code:

 void Array_Loop( int *Array, int n, int L ) ;

int Is_Prime( int Number ) ;

int main( int argc, char *argv[] ){

  int **Array ;
  int n, L ;

  n = atoi( argv[1] ) ;
  L = atoi( argv[2] ) ;

  Matrix_Build( &Array, n, n ) ;
  Array_Loop( Array, n, L ) ;


  return 0 ;

}

void Array_Loop( int *Array, int n, int L ){

  int i, j, k, h ;
  int lctn, move;

  lctn = n / 2 + 1 ;
  i = lctn ;
  j = lctn ;
  move = 1

  while( i != 0 && j != n ){

    for( j = lctn ; j < lctn + move ; j++ ){

         if( L % 2 == 2) Array[i][j] = -1 ;
         else Array[i][j] = Is_Prime( L ) ;
         L++ ;
    }

    move = move * -1 ;

    for( i = i ; i > lctn - move ; i-- ){

      if( L % 2 == 2) Array[i][j] = -1 ;
      else Array[i][j] = Is_Prime( L ) ;
      L++ ;
    }

    move-- ;

    for( j = j ; j > lctn - move ; j-- ){

      if( L % 2 == 2) Array[i][j] = -1 ;
      else Array[i][j] = Is_Prime( L ) ;
      L++ ;
    }

    move = move * -1 ;

    for( i = i ; i < lctn - move ; i-- ){

      if( L % 2 == 2) Array[i][j] = -1 ;
      else Array[i][j] = Is_Prime( L ) ;
      L++ ;
    }

    move++ ;

  }

}


int Is_Prime( int Number ){

  int i ;

  for( i = 0 ; i < Number / 2 ; i++ ){

    if( Number % i != 0 ) return -1 ;

  }

  return Number ;

}
0

2 Answers

You are getting Floating point exception because Number % i, when i is 0:

int Is_Prime( int Number ){

  int i ;

  for( i = 0 ; i < Number / 2 ; i++ ){

    if( Number % i != 0 ) return -1 ;

  }

  return Number ;

}

Just start the loop at i = 2. Since i = 1 in Number % i it always be equal to zero, since Number is a int.

0

Floating Point Exception happens because of an unexpected infinity or NaN. You can track that using gdb, which allows you to see what is going on inside your C program while it runs. For more details:

In a nutshell, these commands might be useful...

gcc -g myprog.c

gdb a.out

gdb core a.out

ddd a.out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.