Can someone find the error "floating point exception" in this program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    Can someone find the error "floating point exception" in this program

    Can someone explain why is my code giving error "floating point exception"
    c
    I was trying to solve a problem but i was getting some error i debugged but cannot find can someone find it.

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>

    long long int fact(int n)
    {
    long long int s=1,i;

    if(n==0||n==1)
    return(1);
    else {
    for(i=1;i<=n;i+ +) {
    s=s*i;
    }

    // printf("%lld ",s);
    return(s);
    }
    }

    int comb(int m,int n)
    {
    float data;
    data=fact(m+n)/(fact(m)*fact(n ));

    // printf("%d %d data:%d ",m,n,data) ;
    return(data);
    }

    int main() {
    float nu[100];
    int t,i,n,a,b,w,j,k ,l,c[100];

    scanf("%d",&t);
    for(j=0;j<t;j++ ) {
    scanf("%d",&n);

    for(i=0;i<=n;i+ +) {
    a=i,b=n-i;
    w=comb(a,b);
    nu[i]=w;
    c[i]=i;
    }

    for(k=0 ;k< i;k++) {
    for(l=0;l<i-1;l++) {
    if(nu[l]>nu[l+1]) {
    int p,q;

    p=nu[l];
    nu[l]=nu[l+1];
    nu[l+1]=p;
    q=c[l];
    c[l]=c[l+1];
    c[l+1]=q;
    }

    if(nu[l]==nu[l+1]) {
    int q;

    if(c[l]>c[l+1]) {
    q=c[l];
    c[l]=c[l+1];
    c[l+1]=q;
    }
    }
    }
    }

    for(l=0 ; l < i ;l++) {
    //printf("%d",nu[i]);
    printf("%d ",c[l]);
    printf("%d\n",n-c[l]);
    }
    }

    return 0;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    If had used CODE tags then I could refer to specific line numbers in your code.
    1. Function fact(). What should happen if n is negative?
    2. Function fact(). What should happen if n is large enough that it's factorial cannot be represented in a long long?
    3. Function comb. Function is defined to return an int, but it returns data, which is a float.
    4. Function comb. The equation for data has all long long terms, so it is evaluated using integer math. You want it evaluated with floating point math.
    5. Function comb. There are few reasons any more to use float. I suggest you use double.
    6. Function comb. Printf uses %d for data. That is not the right code for float (or double).
    7. Function main. Suggest use double rather than float for nu.
    8. Function main. Use p to swap nu elements, but p is int and nu is float (or double).
    9. Function main. Checks if two nu elements are equal. Unwise to compare floating point numbers for equality.
    10. Function main. Printf uses %d for nu. That is not the right code for float (or double).

    Comment

    Working...