943,657 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 561
  • C RSS
Jun 1st, 2009
0

Help with series

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhagyaraj is offline Offline
5 posts
since Jun 2009
Jun 1st, 2009
1

Re: Help with series

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jun 1st, 2009
0

Re: Help with series

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 1st, 2009
0

Re: Help with series

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhagyaraj is offline Offline
5 posts
since Jun 2009
Jun 1st, 2009
0

Re: Help with series

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 .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhagyaraj is offline Offline
5 posts
since Jun 2009
Jun 1st, 2009
0

Re: Help with series

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jun 1st, 2009
0

Re: Help with series

  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhagyaraj is offline Offline
5 posts
since Jun 2009
Jun 1st, 2009
0

Re: Help with series

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. }
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jun 1st, 2009
0

Re: Help with series

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 2nd, 2009
0

Re: Help with series

  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhagyaraj is offline Offline
5 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Simple program understanding pointers ??
Next Thread in C Forum Timeline: help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC