473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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;
}
Apr 18 '15 #1
1 1348
donbock
2,426 Expert 2GB
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).
Apr 18 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Yang Zhang | last post by:
Here is a program: ///////////////////////////////////////////////// #include <iostream> using namespace std ; class A { int a ; A(const A& aA) { a=aA.a ; cout<<"copy constructor...
0
by: Sam Fields | last post by:
I have found very little regarding the error "Unable to find an entry point named EnumerateSecurityPackagesW in DLL security.dll. ". I have an ASP.NET Web Service being accessed via SSL. I found...
1
by: TRI_CODER | last post by:
I am trying to solve the following exception. The exception occurs when my ASP.NET code behind code attemtps to access a remore site using SSL. Please note that all certificates are valid and the...
6
by: Prashant Bhuptani | last post by:
Hi Guys, I am trying to use a C++ dll in VB.NET code. I have imported the dll in the following manner: <code> Imports System.Runtime.InteropServices
3
by: Saman | last post by:
I have a third party dll and I am sure that it is not an activeX or dotnet assembly . I have check it up with Dependency Walker software and found the list of it's exported function in C++ syntax...
30
by: Anarki | last post by:
The following is the program i am trying to compile //restrict.c #include <stdio.h> int main() { char arr = "Qualifiers" char * restrict p = arr; int i = 0; for(; i < 10; ++i)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.