473,416 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

use sharps approximation formula to calculate PI

This is my first ever program. I have struggled big time and its token so long my university lab is closed, can someone please check this over and maybe even correct some parts. I would be so happy if someone could. Thanks

The assignment is to use sharps approximation formula to calculate PI, which is a continous formula until n = k. The second part is to use M_PI and take away the approxiamtion formula, and print both.



Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main (void)
  5. {
  6.    float part1, part2, part3, numerator, n;
  7.    int k;
  8.  
  9.    printf ("Please input number of iterations");
  10.    scanf("%d",&n);
  11.  
  12.    for (k = 0; k <= n; k++);
  13.    {
  14.       if (k%)
  15.          numerator = -1.0;
  16.       else numerator =1.0;
  17.  
  18.       part1 += numerator / (3pow k)* ((2* k) +1);
  19.  
  20.       part2 = 3.464101615 * part1
  21.  
  22.       part3 = M_PI - part2
  23.  
  24.       printf ("%f, %f", part2, part3);
  25.  
  26.    }
  27. }
Feb 2 '07 #1
12 3763
willakawill
1,646 1GB
Here are the errors in your code after I have repaired some typos:
Expand|Select|Wrap|Line Numbers
  1. int main (void)
  2.  
  3. {
  4.     float part1, part2, part3, numerator, n;
  5.     int k;
  6.  
  7.     printf ("Please input number of iterations");
  8.     scanf("%d",&n);
  9.  
  10.     for (k = 0; k <= n; k++);
  11.     {
  12.         if (k%)
  13.             numerator = -1.0;
  14.         else numerator =1.0;
  15.  
  16.         part1 += numerator / (3pow k)* ((2* k) +1);
  17.  
  18.         part2 = 3.464101615 * part1;
  19.  
  20.         part3 = M_PI - part2;
  21.  
  22.         printf ("%f, %f", part2, part3);
  23.  
  24.     }
  25.     return 0;
  26. }
Expand|Select|Wrap|Line Numbers
  1. Testall.cpp(19) : error C2059: syntax error : ')'
  2. Testall.cpp(21) : error C2181: illegal else without matching if
  3. Testall.cpp(23) : error C2059: syntax error : 'bad suffix on number'
  4. Testall.cpp(23) : error C2146: syntax error : missing ')' before identifier 'pow'
  5. Testall.cpp(23) : error C2059: syntax error : ')'
  6. Testall.cpp(27) : error C2065: 'M_PI' : undeclared identifier
Feb 2 '07 #2
Could someone help me by fixing these fault codes or even tell me what they mean and how to fix them. I am a real novice at this. Thank you.
Feb 2 '07 #3
willakawill
1,646 1GB
Could someone help me by fixing these fault codes or even tell me what they mean and how to fix them. I am a real novice at this. Thank you.
So where did you get the code that you posted?
Feb 2 '07 #4
horace1
1,510 Expert 1GB
Could someone help me by fixing these fault codes or even tell me what they mean and how to fix them. I am a real novice at this. Thank you.
you are getting close - i have fixed a couple more errors and it now compiles, see if it works!
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main (void)
  5.  
  6. {
  7.     float part1, part2, part3, pow3=1, numerator, n;
  8.     int k;
  9.  
  10.     printf ("Please input number of iterations");
  11.     scanf("%d",&n);
  12.  
  13.     for (k = 1; k <= n; k++);
  14.     {
  15.         if (k%2)                  // ** added 2
  16.             numerator = -1.0;      // ** odd
  17.         else numerator =1.0;       // ** even
  18.                 pow3=  pow3*3;            // ** added
  19.         part1 += numerator / (pow3)* ((2* k) +1);
  20.   }
  21.         part2 = 3.464101615 * part1;
  22.  
  23.         part3 = M_PI - part2;
  24.  
  25.         printf ("%f, %f", part2, part3);
  26.  
  27.     // remove }
  28.     return 0;
  29. }
  30.  
Feb 2 '07 #5
Well the program I tried to write came from various sources. The notes I have are very basic and I found a couple of examples on the net. They were not the examples I was looking for, but I thought I could modify them and hope they would work. I know its not the best idea, and I am planning on buying a couple of books on Monday to help me, but until then, am realy stuck.
Feb 2 '07 #6
horace1
1,510 Expert 1GB
Well the program I tried to write came from various sources. The notes I have are very basic and I found a couple of examples on the net. They were not the examples I was looking for, but I thought I could modify them and hope they would work. I know its not the best idea, and I am planning on buying a couple of books on Monday to help me, but until then, am realy stuck.
you can check your results by looking at section 2.2.1 of
http://numbers.computation.free.fr/C...piclassic.html
where it gives PI with 1, 2, 5 and 10 iterations
Feb 2 '07 #7
Thanks for your help, you have made my day. One more question, can you recommend any good C++ program books. I would love to learn more about this.
Thanks again.
Feb 2 '07 #8
horace1
1,510 Expert 1GB
Thanks for your help, you have made my day. One more question, can you recommend any good C++ program books. I would love to learn more about this.
Thanks again.
there are still some semantic errors in the last code I posted, e.g. look at the scanf() and remove the ; on the end of the for()

have a look at this tutorial
http://www.cplusplus.com/doc/tutorial/
Feb 2 '07 #9
I am having a slight problem with the ouput results. I have traced it back to the pow3, this section has to be 3 to the power of k, which will increase on every interaction, until it matches the number of interations the user has put in. How would I be able to do this.
Feb 5 '07 #10
horace1
1,510 Expert 1GB
I am having a slight problem with the ouput results. I have traced it back to the pow3, this section has to be 3 to the power of k, which will increase on every interaction, until it matches the number of interations the user has put in. How would I be able to do this.
fixed a couple of more errors (indicated by comments), try it now
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main (void)
  5.  
  6. {
  7.     double part1=1, part2, part3, pow3=1, numerator;
  8.     int k, n;   // ** changed n to an int to suit the scanf() and for()
  9.  
  10.     printf ("Please input number of iterations");
  11.     scanf("%d",&n);
  12.  
  13.     for (k = 1; k < n; k++)//; removed ;
  14.     {
  15.         if (k%2)                  // ** added 2
  16.             numerator = -1.0;      // ** odd
  17.         else numerator =1.0;       // ** even
  18.         pow3=  pow3*3;            // ** added
  19.         part1 += numerator / (pow3* ((2* k) +1));
  20.          printf ("%f, %d\n", pow3, ((2*k)+1));
  21.       }
  22.         part2 = 3.464101615 * part1;
  23.  
  24.         part3 = M_PI - part2;
  25.  
  26.         printf ("%f, %f", part2, part3);
  27. getchar();getchar();
  28.     // remove }
  29.     return 0;
  30. }
  31.  
Feb 5 '07 #11
I have just tried it. It works perfectly, thanks. Your a star horace1.

Thanks again.

James
Feb 6 '07 #12
ashith
8
Nice program
Feb 6 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
2
by: celsius | last post by:
Hi folks, Al Bowers wrote this program on comp.lang.c Date: 2001-07-09 13:41:58 PST #include <stdio.h> int isleap (unsigned yr); static unsigned months_to_days (unsigned month); static long...
2
by: Gálos Zsuzsa | last post by:
Hi all, I need to calculate Formulas. For example: dim strFormula as string="25*(12-6)" I ned a DotNet Function to calculate this Formula (25*(12-6)=150): dim dblValue as double = 150
2
by: Ivan | last post by:
Hi All, I just wanna ask one simply question.. Is there anyone know how to calculate the output value from a string like that?? Dim strFormula As String = "20+30+(100+20+30)*0.1" Dim...
8
by: GB | last post by:
Hello, How to calculate value for the following formula (I need C# code): res = (((m+1)(m+2)...(m+(k-1)))/1.2...(k-1)) or more generalized formula is: k-1 __ | | (m+i)
11
by: Lloydm | last post by:
Not that of a guru with excel but tryin to write a formula that will calculate total hrs worked for normal mon-fri 9a-5p week schedule. I have figured out how to calculate daily totals but for week...
7
by: mturner64 | last post by:
I need the formula to calculate the girth of a cone. I am not having any luck. Anyone know the formula? Thanks, Mike
1
by: Victor | last post by:
Can someone help me with the formula(s) to calculate the RGB components for a gradient from green to red? I'm trying to display a nice graphic with a gradient that reflects the profit or loss and I...
7
by: company1484 | last post by:
I've been given an assignment to approximate the value of sine of an angle in degrees by using the formula: sin(x) = x −x3/3!+x5/5!−x7/7!+x9/9!· · · x is an angle in radians. He wants the program...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.