Help with series

Thread Solved

Join Date: Jun 2009
Posts: 5
Reputation: bhagyaraj is an unknown quantity at this point 
Solved Threads: 0
bhagyaraj bhagyaraj is offline Offline
Newbie Poster

Help with series

 
0
  #1
Jun 1st, 2009
Hello,I need help with the following series (sorry I forgot what its called..)

1/1! - 2/2! + 3/3! -4/4!....upto n terms

Here's my program :
  1. #include<conio.h>
  2. #include<stdio.h>
  3. #include<float.h>
  4.  
  5. void main()
  6. {
  7. float com=0,j=0,j1=0,ans=0; //variable decl of float type
  8. int tillw,i,no=0,fac1=1,fac=1,f1,f2; //variable decl of int type
  9. clrscr();
  10. printf("\n\n\t\t *********************************");
  11. printf("\n\t\t ---------------------------------");
  12. printf("\n\n\t\t SERIES");
  13. printf("\n\n\t\t ---------------------------------");
  14. printf("\n\t\t *********************************");
  15. printf("\n\n\n\n\t\t Enter till where you want : ");
  16. scanf("%d",&tillw);
  17. flushall(); //accept no. from user
  18. for(i=1;i<=tillw;i++)
  19. {
  20. no=no+1;
  21. ans=no%2;
  22. if(ans!=0)
  23. {
  24. //addition part of series
  25. for(f1=1;f1<=no;f1++)
  26. {
  27. fac=fac*f1;
  28.  
  29. } //find factorial
  30. j=no/fac;
  31. com=com+j;
  32.  
  33. }
  34. else
  35. {
  36. //subtraction part of series
  37. for(f2=1;f2<=no;f2++)
  38. {
  39. fac1=fac1*f2;
  40.  
  41. } //find factorial
  42. j1=no/fac1;
  43. com=com-j1;
  44. }
  45.  
  46. //printf("\n\n\t\t The answer is : %0.2f",com);(for testing answer in each step)
  47. } //main for-loop ends..
  48. printf("\n\n\t\t The answer is : %f",com); //prints final answer
  49. getch();
  50. } //void-main ending..


So now my problem is,whatever input I give the answer is always 0.00


PS:Realized that it gives correct answer till 2,but after that whatever the input it gives 0.00 as the answer
Last edited by bhagyaraj; Jun 1st, 2009 at 1:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Help with series

 
1
  #2
Jun 1st, 2009
Aaaaaaaaaaargh! void main() , don't use it, it's evil (check this page) !!
Use int main() instead

You should also read this.

A comment like this: //variable decl of float type makes no sense, everyone can see that the declared variables are of type float.

Note:: This code looks more like C code, it's definitely no C++ !!
Last edited by tux4life; Jun 1st, 2009 at 1:32 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help with series

 
0
  #3
Jun 1st, 2009
See: you have fac > no for all no values because fac = n!. Therefore no/fac == 0 (integer division) and com=com+j is the same as com += 0...

1. Use double type for math calculations (not float).
2. Use floating-point division if you want to get floating-point result, for example:
  1. com += (double)n/fac;
3. You will get (unsignalled) integer overflow in factorial calculation: 13! is greater than max integer value.

Better search DaniWeb for series: there are lots of threads on this topic. It's a bad approach to calculate factorial for every series member...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: bhagyaraj is an unknown quantity at this point 
Solved Threads: 0
bhagyaraj bhagyaraj is offline Offline
Newbie Poster

Re: Help with series

 
0
  #4
Jun 1st, 2009
I changed void main to int main, also using double for calculations..
Still the same error

ArkM,what exactly you mean by saying tht it is bad to calculate factorial for each no of series.I did not understand

And also can you plz explain

See: you have fac > no for all no values because fac = n!. Therefore no/fac == 0 (integer division) and com=com+j is the same as com += 0...

I could not understand understand..sorry if i am becoming a headache..

soryy for the double post,how to delete my post btw?
Last edited by bhagyaraj; Jun 1st, 2009 at 2:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: bhagyaraj is an unknown quantity at this point 
Solved Threads: 0
bhagyaraj bhagyaraj is offline Offline
Newbie Poster

Re: Help with series

 
0
  #5
Jun 1st, 2009
I changed void main to int main, also using double for calculations..
Still the same error

ArkM,what exactly you mean by saying that it is bad to calculate factorial for each no. of series.I did not understand

And also can you plz explain ...

See: you have fac > no for all no values because fac = n!. Therefore no/fac == 0 (integer division) and com=com+j is the same as com += 0...

I could not understand...sorry if i am becoming too much of a nuisance .
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Help with series

 
0
  #6
Jun 1st, 2009
you dont need to recalculate each and every factorial. because if you've already calculated 9! (for example), then 10! is really just 9! * 10.

and basically it's this: you can't do integer division. because all your quotients will be integer zeros.

3/6 = 0. 4/24 = 0. 5/120 = 0.

you have to use the "double" data type.

and as he already mentioned, it will crap out at 13!, because the maximum unsigned integer 2^32 = ~4 billion.

look into "long long int" data types. and "big number" libraries if you really need to get deep.

power series are not a trival matter in C.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: bhagyaraj is an unknown quantity at this point 
Solved Threads: 0
bhagyaraj bhagyaraj is offline Offline
Newbie Poster

Re: Help with series

 
0
  #7
Jun 1st, 2009
  1. #include<conio.h>
  2. #include<stdio.h>
  3. #include<float.h>
  4.  
  5. int main()
  6. {
  7. double com=0,j=0,j1=0,ans=0;
  8. int no=0;
  9. double tillw,i,fac1=1,fac=1,f1,f2;
  10. clrscr();
  11. printf("\n\n\t\t *********************************");
  12. printf("\n\t\t ---------------------------------");
  13. printf("\n\n\t\t SERIES");
  14. printf("\n\n\t\t ---------------------------------");
  15. printf("\n\t\t *********************************");
  16. printf("\n\n\n\n\t\t Enter till where you want : ");
  17. scanf("%d",&tillw);
  18. flushall(); //accept no. from user
  19. for(i=1;i<=tillw;i++)
  20. {
  21. no=no+1;
  22. ans=no%2;
  23. if(ans!=0)
  24. {
  25. //addition part of series
  26. fac=fac*i;
  27. j=(double)no/fac;
  28. com=(double)com+j;
  29.  
  30. }
  31. else
  32. {
  33. //subtraction part of series
  34. fac=fac*i;
  35. j1=(double)no/fac1;
  36. com=(double)com-j1;
  37. }
  38.  
  39. //printf("\n\n\t\t The answer is : %0.2f",com);(for testing answer in each step)
  40. } //main for-loop ends..
  41. printf("\n\n\t\t The answer is : %f",com); //prints final answer
  42. getch();
  43. return (0);
  44. } //void-main ending..

I removed the for loops for calculating the factorials,used double and removed void main.STill the same problem

Oh i dont need to go to such high numbers.I will be happy if it can calculate upto 5
Last edited by bhagyaraj; Jun 1st, 2009 at 3:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Help with series

 
0
  #8
Jun 1st, 2009
The series breaks down to::
1 - 2 / (2 *1) +3 / (3*2!) -....
i.e 1 - 1 + 1/2! - 1/3!
So the first (major) 2 terms of the series cancel out.
Now, may be you define a variable
  1. int sign_carrier = 1 ;
  2. long fact = 1;
  3. double sum = 0;
  4. for( i = 2;i<=tillw;i++) /*start from 2*/
  5. /*start from 3rd term in the series*/
  6. {
  7. if (i is even) sign_carrier =1;
  8. else sign_carrier =-1;
  9.  
  10. now get the factorial with the help of i;
  11. sum = sum + (sign_carrier / fact);
  12. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help with series

 
0
  #9
Jun 1st, 2009
Well, let f(i) is i-th member without sign (it's not C).
  1. f(i) = i/i! = i/(1*2*...*(i-1)*i) =
  2. i/(i*(1*2...*(i-1)) = 1/(i-1)!
  3. f(i+1) = 1/i! = 1/((i-1)!*i) = f(i)/i
  4. i = 1, 2,... (0! = 1 by definition)
In other words, if we know i-th member of series then the next member is equal to this member / i (ignore sign).
if we know that i-th member is positive then the next member is negative and vice versa.
We know that 2-nd member is equal to -1.0 (negative). We know that the partial sum of the series is equal to 1.0 at this moment: f(1) == 1/1!...

Now we must define calculation loop condition. Obviously no sense to continue calculations when the next member of series is too small: double type precision is ~16 decimal digits. We want to stop calculations when f(i) < eps, where eps == 1e-16 (for example).

Yet another tip: no need in int variables at all!

  1. double Series()
  2. {
  3. const double eps = 1e-16;
  4. double sum = 1.0; /* sum(1) */
  5. double f, i;
  6. int neg = 1;/* the 2nd member < 0 */
  7. for (f = 1.0, i = 2.0; f > eps; f /= i, i += 1.0) {
  8. /* cout << i << '\t' << f << '\t'; */
  9. sum += (neg? -f: f);
  10. /* cout << setprecision(15) << sum << '\t'
  11.   << (sum?1/sum:0) << '\n'; */
  12. neg ^= 1;
  13. }
  14. /* cout << sum << endl; */
  15. return sum;
  16. }
The answer: 0.367879441171442
It's equal to 1/e constant. Exactly.
Last edited by ArkM; Jun 1st, 2009 at 6:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: bhagyaraj is an unknown quantity at this point 
Solved Threads: 0
bhagyaraj bhagyaraj is offline Offline
Newbie Poster

Re: Help with series

 
0
  #10
Jun 2nd, 2009
  1. #include<conio.h>
  2. #include<stdio.h>
  3.  
  4. int main()
  5. {
  6. int sign_carrier,i,tillw,ans;
  7. long int fac=1;
  8. float sum=0;
  9. clrscr();
  10. printf("\n\n\t\t *********************************");
  11. printf("\n\t\t ---------------------------------");
  12. printf("\n\n\t\t SERIES");
  13. printf("\n\n\t\t ---------------------------------");
  14. printf("\n\t\t *********************************");
  15. printf("\n\n\n\n\t\t Enter till where you want : ");
  16. scanf("%d",&tillw);
  17. flushall(); //accept no. from user
  18. for(i=1;i<=tillw;i++)
  19. {
  20. ans=i%2; //to find odd or even..
  21. if(ans!=0) //odd
  22. {
  23. sign_carrier=1;
  24. }
  25. else //even
  26. {
  27. sign_carrier=-1;
  28. }
  29. fac=fac*i;
  30. sum+=(double)(i*sign_carrier)/fac;
  31. } //main for-loop ends..
  32. printf("\n\n\t\t Answer is : %0.20f",sum);
  33. getch();
  34. return sum;
  35. } //void-main ending..

This works
Thank you all for your time.really appreciate it..
I have started it from 1 instead of 2 which you guys had suggested
Last edited by bhagyaraj; Jun 2nd, 2009 at 3:52 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC