| | |
approximate the value of PI
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 6
Reputation:
Solved Threads: 0
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
Here is the script that I've written but I'm stuck. Please advice. My alogarithm might be wrong
C Syntax (Toggle Plain Text)
#include <stdio.h> int main (void) { int x, i; //int sum=0; double PI; float sum=0, y; printf("Please enter the terms of series that should be included> "); scanf("%d", &x); for (i=1; i<=x; i++){ i=y; if (i%2==1) //odd if (sum%2==0) sum=sum+1/y; else sum=sum-1/y; } PI= 4*sum; printf("The sum is %f\n", sum); printf("Approximate value of PI is %f\n", PI); return 0; }
•
•
Join Date: Sep 2008
Posts: 5
Reputation:
Solved Threads: 0
Hi,
First of all welcome to the world of computer programming.
Here is ur solution :
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.
First of all welcome to the world of computer programming.
Here is ur solution :
c Syntax (Toggle Plain Text)
for (i=1; i<=x; i++) { if (i%2!=0) sum=sum+1/y; else sum=sum-1/y; y=y+2; }
Last edited by cscgal; Sep 4th, 2008 at 2:21 pm. Reason: Added code tags
Thanks & Regards
Techmighty
Techmighty
•
•
Join Date: Sep 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
Use double (not float) type for math calculations!
c Syntax (Toggle Plain Text)
double x, y, z, sum, pi; ... sum = 1.0; for (i = 0, y = 3.0; i < n; ++i, y += 2.0) { z = 1.0 / y; if ((i & 1) == 0) sum -= z; else sum += z; } pi = 4.0 * sum;
Last edited by ArkM; Sep 4th, 2008 at 8:16 am.
•
•
Join Date: Sep 2008
Posts: 6
Reputation:
Solved Threads: 0
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;
}
Well I compiled the following program and got the following errors:
foo.c
output
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
foo.c
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main(void) { for ( int i = 0; i < 10; i++ ) /*int inside loop*/ { } return 0; }
output
C Syntax (Toggle Plain Text)
user@ubuntu804desktop:~$ gcc -Wall foo.c foo.c: In function ‘main’: 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*
If you want to be cryptic...
*hides*
C Syntax (Toggle Plain Text)
for (i = 0, y = 1.0, sum = 0.0, sign = 1.0 ; i < n; ++i, sum += sign*(1/y), sign *= -1.0, y += 2.0);
![]() |
Similar Threads
- Approximate Value of pi, incorrect results (C++)
- "page cannot be displayed" only w/ router (Networking Hardware Configuration)
- Error creating new ASP.Net project (ASP.NET)
- IE6 has been constantly hijacked by .... (Viruses, Spyware and other Nasties)
- Pi Approximation (C++)
Other Threads in the C Forum
- Previous Thread: Having problems in using linked lists
- Next Thread: Why address Gap in Char?
Views: 1951 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






