2

I compiled ths code, but it gives floating point exception on execution

#include<stdio.h>
int fibonacci(int n)
{
  int a=0,b=1,c;
  while((n-1)>0)
  {
    c=a+b;
    a=b;
    b=c;
    n--;
  }
  return c;
}
int main()
{
  int t,a,b,c;
  scanf("%d",&t);
  while(t--)
  {
    scanf("%d%d",&a,&b);
    while(a--)
    {
      b=fibonacci(b);
    }
    printf("%d",(b%c));
   }
  return 0;
 }

What should I do to resolve this problem?

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
Apoorv Ashutosh
  • 27
  • 1
  • 1
  • 6

4 Answers4

8

What should I do to resolve this problem?

The first thing to address this problem would be to have a look at the hints your compiler is able to provide you. That is, enable warnings (-Wall) and debugging symbols (-g):

$ gcc test.c -Wall -g
test.c: In function ‘main’:
test.c:25:11: warning: ‘c’ may be used uninitialized in this function [-Wuninitialized]

There may be something wrong with the variable c in line 25, which is exactly the print statement:

printf("%d",(b%c));

Let's see what happens when we run it:

$ ./a.out
1
2
3
Floating point exception (core dumped)

Ah, well, it fails. But how did it crash? That's a use case for gdb:

$ gdb ./a.out
GNU gdb (GDB) Fedora (7.5.1-37.fc18)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/moooeeeep/a.out...done.
(gdb) run
Starting program: /home/moooeeeep/a.out 
1
2
3

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400640 in main () at test.c:25
25      printf("%d",(b%c));
Missing separate debuginfos, use: debuginfo-install glibc-2.16-30.fc18.x86_64

Just at line 25. Suspicious. Let's examine the contents of the variables:

(gdb) print b
$1 = 1
(gdb) print c
$2 = 0

That variable c is indeed zero. But why is it causing a floating point exception when there are only integers? (Others have observed this before you.) As you can see in the debugger it's called an arithmetic exception (c.f.):

The SIGFPE signal reports a fatal arithmetic error. Although the name is derived from “floating-point exception”, this signal actually covers all arithmetic errors, including division by zero and overflow.

Let's see what valgrind tells us:

$ valgrind ./a.out
==3113== Memcheck, a memory error detector
==3113== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3113== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==3113== Command: ./a.out
==3113== 
1
2
3
==3113== 
==3113== Process terminating with default action of signal 8 (SIGFPE)
==3113==  Integer divide by zero at address 0x403E58A5B
==3113==    at 0x400640: main (test.c:25)
==3113== 
==3113== HEAP SUMMARY:
==3113==     in use at exit: 0 bytes in 0 blocks
==3113==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3113== 
==3113== All heap blocks were freed -- no leaks are possible
==3113== 
==3113== For counts of detected and suppressed errors, rerun with: -v
==3113== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Floating point exception (core dumped)

valgrind identified an integer division by zero exactly at line 25. You should have an eye on that line (and the variables involved)!


Note that most IDEs (e.g., Eclipse) have (some of) these tools directly integrated, which makes debugging really a charm.

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
3

You did not initialize c, so you are doing modulo some undefined number, probably 0:

printf("%d",(b%c));
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

In addition to the problem stated by Shafik, fibonacci(0) and fibonacci(1) will fail because the assumption that it's okay to leave c in int fibonacci(int) uninitialized since it's being set in the loop is a bad one for n = 0 and n = 1 since the while loop will be skipped and thus you will be returning an uninitialized variable.

user123
  • 8,970
  • 2
  • 31
  • 52
1

When passing 1 to fibonacci(int n) the method returns c without initializing it.

Try this:

int fibonacci(int n)
{
  if(n==0 || n==1)
    return n;

  int a=0,b=1,c;
  while((n-1)>0)
  {
    c=a+b;
    a=b;
    b=c;
    n--;
  }
  return c;
}
Jim Buck
  • 2,383
  • 23
  • 42