approximate the value of PI

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 6
Reputation: MadnessCow is an unknown quantity at this point 
Solved Threads: 0
MadnessCow MadnessCow is offline Offline
Newbie Poster

approximate the value of PI

 
0
  #1
Sep 4th, 2008
Hi guys. I'm pretty new to C program. I've been given this assignment to write a program that asks the user thow many terms of the serires equation to use in approximating PI. The formula given is pi=4X(1- 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ....)

Here is the script that I've written but I'm stuck. Please advice. My alogarithm might be wrong

  1. #include <stdio.h>
  2. int main (void)
  3. {
  4. int x,
  5. i;
  6. //int sum=0;
  7. double PI;
  8. float sum=0, y;
  9.  
  10. printf("Please enter the terms of series that should be included> ");
  11. scanf("%d", &x);
  12.  
  13. for (i=1; i<=x; i++){
  14. i=y;
  15.  
  16. if (i%2==1) //odd
  17.  
  18. if (sum%2==0)
  19. sum=sum+1/y;
  20. else
  21. sum=sum-1/y;
  22. }
  23.  
  24.  
  25. PI= 4*sum;
  26. printf("The sum is %f\n", sum);
  27. printf("Approximate value of PI is %f\n", PI);
  28.  
  29. return 0;
  30. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 5
Reputation: techmighty is an unknown quantity at this point 
Solved Threads: 0
techmighty techmighty is offline Offline
Newbie Poster

Re: approximate the value of PI

 
0
  #2
Sep 4th, 2008
Hi,

First of all welcome to the world of computer programming.
Here is ur solution :
  1. for (i=1; i<=x; i++)
  2. {
  3.  
  4. if (i%2!=0)
  5. sum=sum+1/y;
  6. else
  7. sum=sum-1/y;
  8.  
  9. y=y+2;
  10. }
Initial value of sum and y should be 0 and 1 respectively. Rest of ur program was ok. I haven't compiled this code. I have just put logic. Please let me know if this code doesn't work.
Last edited by cscgal; Sep 4th, 2008 at 2:21 pm. Reason: Added code tags
Thanks & Regards
Techmighty
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: MadnessCow is an unknown quantity at this point 
Solved Threads: 0
MadnessCow MadnessCow is offline Offline
Newbie Poster

Re: approximate the value of PI

 
0
  #3
Sep 4th, 2008
I could run it but the answer is wrong. I think i know where went wrong but i can't figure out the solution. The sum should be less than 1. But since the sum is declared as int. So it cannot take decimal places. So what should i change to convert sum from int to float?
Last edited by MadnessCow; Sep 4th, 2008 at 7:26 am.
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: approximate the value of PI

 
1
  #4
Sep 4th, 2008
Use double (not float) type for math calculations!
  1. double x, y, z, sum, pi;
  2. ...
  3. sum = 1.0;
  4. for (i = 0, y = 3.0; i < n; ++i, y += 2.0)
  5. {
  6. z = 1.0 / y;
  7. if ((i & 1) == 0)
  8. sum -= z;
  9. else
  10. sum += z;
  11. }
  12. pi = 4.0 * sum;
Last edited by ArkM; Sep 4th, 2008 at 8:16 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,275
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: approximate the value of PI

 
0
  #5
Sep 4th, 2008
Maybe you need to do something like:

1.0/y;
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: MadnessCow is an unknown quantity at this point 
Solved Threads: 0
MadnessCow MadnessCow is offline Offline
Newbie Poster

Re: approximate the value of PI

 
0
  #6
Sep 4th, 2008
Thank you guys. I've manage to work it out. Here is the code:
#include <stdio.h>
int main (void)
{
int x;
float sum=0;
double PI;

printf("Please enter the terms of series that should be included> ");
scanf("%d", &x);

for (int i=1; i<x; i+=2)
if (i%4==1)
sum=sum+1./(double)i;
else
sum=sum-1./(double)i;

PI= 4*sum;
printf("The sum is %f\n", sum);
printf("Approximate value of PI is %f\n", PI);

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,275
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: approximate the value of PI

 
0
  #7
Sep 4th, 2008
I would change the sum to a double as well.

The int i needs to be declared outside the for loop I believe is it C99 mode or something.


And to increase the print precision you could do %.9f
Last edited by iamthwee; Sep 4th, 2008 at 8:36 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: MadnessCow is an unknown quantity at this point 
Solved Threads: 0
MadnessCow MadnessCow is offline Offline
Newbie Poster

Re: approximate the value of PI

 
0
  #8
Sep 4th, 2008
I think the int declared inside the loops is also fine. I ran this program and everything works accordingly. Btw, what are the differences between float and double?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,275
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: approximate the value of PI

 
0
  #9
Sep 4th, 2008
Well I compiled the following program and got the following errors:

foo.c
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6.  
  7. for ( int i = 0; i < 10; i++ ) /*int inside loop*/
  8. {
  9.  
  10. }
  11. return 0;
  12. }

output

  1. user@ubuntu804desktop:~$ gcc -Wall foo.c
  2. foo.c: In function ‘main’:
  3. foo.c:7: error: ‘for’ loop initial declaration used outside C99 mode

I believe you can suppress this error if you compile with the necessary flags. -sdt=c99

> double or float?
http://www.gidforums.com/t-2934.html
Last edited by iamthwee; Sep 4th, 2008 at 11:08 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: approximate the value of PI

 
0
  #10
Sep 4th, 2008
If you want to be cryptic...
  1. for (i = 0, y = 1.0, sum = 0.0, sign = 1.0 ;
  2. i < n;
  3. ++i, sum += sign*(1/y), sign *= -1.0, y += 2.0);
*hides*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 1951 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC