473,394 Members | 1,746 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,394 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 1352
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.