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;
}

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.

commented: yes :) +17

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...

rubberman, if I understand you correctly, you say that 0%2 causes an 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!

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.