what is FPE?

how do i correct this in my program....tried everything...:sad:

#include<iostream>
using namespace std;
int factorial(int a)
{int f=1;
    for(int i=1;i<a+1;i++)
    {
        f*=i;
    }
    return f;
}
int main()
{
    int factorial(int);
    int n;cin>>n;int x=n/2,y=n%2,d,e,f,sum=0;
    for(int i=0;i<n;i++)
    {
        d=factorial(x+y);f=factorial(y);
        if(x==0)
        {
            e=1;
        }
        else
        {
         e=factorial(x);
        }
        if(y==0)
        {
            f=1;
        }
        else{
            f=factorial(y);
        }
        sum=sum+(d/(e*f));x--;y=y+2;
    }
    cout<<sum;
}

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Please change the cin statement to hard coded values 1) so we can exactly reproduce it and 2) so we know which values caused the problem. FPE means you are probably dividing by zero.

1. Please don't make such ugly one-liners like lines #14 and #33

2. I don't know, if this helps, but x definitely goes into negatives, which might cause your factorial function to do strange things.

Comments
yes :)

Line 14: int n;cin>>n;int x=n/2,y=n%2,d,e,f,sum=0; If 'n' evaluates to 0, then n/2 == 0, ditto n%2. BOOM! FPE...

You are right! Doh! My bad. I think my mathematical brain cells took the day off, and I need to get them awake for programming some differential equations in the next few weeks! Likewise, 0/2 is ok as well. Gah! Sorry for the confusion. It must be mathematical dyslexia at work!

Edited 4 Years Ago by rubberman: n/a

prabh94, you do realise, that a factorial increases very fast?
20!~1.216e17 where the maximum value for int is 32 768. That's too little even for 10!

This article has been dead for over six months. Start a new discussion instead.