Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 21-03-2009, 02:06 PM   #1 (permalink)
Alpha Geek
 
ajaybc's Avatar
 
Join Date: Feb 2007
Location: Cochin
Posts: 744
Default Please Debug this C++ program


I made this program for printing the sine series.It is not displaying any errors but just exiting without showing the result.Please help...
Quote:
#include<iostream.h>
#include<conio.h>
#include<math.h>

double fact(double n)
{
if(n!=1)
return n*(n-1);
}

void main()
{
clrscr();
double f,x,s=-1,j,i,n;
cout<<"Enter the number:";
cin>>x;
cout<<"Upto how many terms:";
cin>>n;
j=1;
for(i=1;i<=n;i++)
{
s=s*(-1);
f=fact(j);
x=x+(s*(pow(x,j)/f));
j=j+2;
}
cout<<"Answer:"<<x;
getch();
}
__________________
Available for freelance web design and web development jobs.
You can view my portfolio here.
Ajay Balachandran

Please contact via PM
ajaybc is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 21-03-2009, 02:12 PM   #2 (permalink)
damnbadman666
 
damngoodman999's Avatar
 
Join Date: Nov 2008
Location: Coimbatore
Posts: 2,590
Default Re: Please Debug this C++ program

Stack level problem , take out the DOUBLE
-----------------------------------------
Posted again:
-----------------------------------------
Floating point error ---- INTEGER is used for clearence and output
__________________
AMD Phenom 2 940 @ 3.4Ghz [Hyper 212+] :arrow: MA790GP-UD4H :arrow: OCZ DDR2 2x2GB :arrow: Asus GTX 560ti :bananana::arrow: 1TB SATA2 :arrow: LG 20" LED :arrow: Razer DA mice :arrow: RED HOT cabby

Last edited by damngoodman999; 21-03-2009 at 02:16 PM. Reason: Automerged Doublepost
damngoodman999 is offline  
Old 21-03-2009, 03:48 PM   #3 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: Please Debug this C++ program

Your factorial algorithm is wrong unless I am totally misunderstanding your code. And you don't use floating types in factorials. The factorials are always computed with integers AFAIK.

Code:
double fact(double n)
{
if(n!=1)
return n*(n-1);
}
This should be rather:

Code:
long int fact(int n)
{
if(n>1)
    return n*fact(n-1);
else
    return 1;
}
Maybe this will help.
Also a kindly advice. I suggest tou learn to indent your code. It will drastically improve readability.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline  
Old 21-03-2009, 05:56 PM   #4 (permalink)
Alpha Geek
 
ajaybc's Avatar
 
Join Date: Feb 2007
Location: Cochin
Posts: 744
Default Re: Please Debug this C++ program

^^Thanks.Worked great.
__________________
Available for freelance web design and web development jobs.
You can view my portfolio here.
Ajay Balachandran

Please contact via PM
ajaybc is offline  
Old 22-03-2009, 06:57 PM   #5 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Please Debug this C++ program

Next up, one liners. Same thing.
Code:
long int fact(int n) { return n>1?n*fact(n-1):1; }
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Sujeet
- by gforz
- by soumya

Advertisement




All times are GMT +5.5. The time now is 03:10 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2