use sharps approximation formula to calculate PI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • james121285
    New Member
    • Feb 2007
    • 18

    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.



    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    {
       float part1, part2, part3, numerator, n;
       int k;
    
       printf ("Please input number of iterations");
       scanf("%d",&n);
    
       for (k = 0; k <= n; k++);
       {
          if (k%)
             numerator = -1.0;
          else numerator =1.0;
    
          part1 += numerator / (3pow k)* ((2* k) +1);
    
          part2 = 3.464101615 * part1
    
          part3 = M_PI - part2
    
          printf ("%f, %f", part2, part3);
    
       }
    }
    Last edited by Ganon11; Feb 2 '07, 08:04 PM. Reason: code tags added, indented
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Here are the errors in your code after I have repaired some typos:
    Code:
    int main (void)
    
    {
    	float part1, part2, part3, numerator, n;
    	int k;
    
    	printf ("Please input number of iterations");
    	scanf("%d",&n);
    
    	for (k = 0; k <= n; k++);
    	{
    		if (k%)
    			numerator = -1.0;
    		else numerator =1.0;
    
    		part1 += numerator / (3pow k)* ((2* k) +1);
    
    		part2 = 3.464101615 * part1;
    
    		part3 = M_PI - part2;
    
    		printf ("%f, %f", part2, part3);
    
    	}
    	return 0;
    }
    Code:
    Testall.cpp(19) : error C2059: syntax error : ')'
    Testall.cpp(21) : error C2181: illegal else without matching if
    Testall.cpp(23) : error C2059: syntax error : 'bad suffix on number'
    Testall.cpp(23) : error C2146: syntax error : missing ')' before identifier 'pow'
    Testall.cpp(23) : error C2059: syntax error : ')'
    Testall.cpp(27) : error C2065: 'M_PI' : undeclared identifier

    Comment

    • james121285
      New Member
      • Feb 2007
      • 18

      #3
      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.

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by james121285
        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?

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by james121285
          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!
          Code:
          #include <stdio.h>
          #include <math.h>
          
          int main (void)
          
          {
          	float part1, part2, part3, pow3=1, numerator, n;
          	int k;
          
          	printf ("Please input number of iterations");
          	scanf("%d",&n);
          
          	for (k = 1; k <= n; k++);
          	{
          		if (k%2)                  // ** added 2
          			numerator = -1.0;      // ** odd
          		else numerator =1.0;       // ** even
                          pow3=  pow3*3;            // ** added
          		part1 += numerator / (pow3)* ((2* k) +1);
            }
          		part2 = 3.464101615 * part1;
          
          		part3 = M_PI - part2;
          
          		printf ("%f, %f", part2, part3);
          
          	// remove }
          	return 0;
          }

          Comment

          • james121285
            New Member
            • Feb 2007
            • 18

            #6
            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.

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              Originally posted by james121285
              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

              where it gives PI with 1, 2, 5 and 10 iterations

              Comment

              • james121285
                New Member
                • Feb 2007
                • 18

                #8
                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.

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  Originally posted by james121285
                  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

                  Comment

                  • james121285
                    New Member
                    • Feb 2007
                    • 18

                    #10
                    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.

                    Comment

                    • horace1
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 1510

                      #11
                      Originally posted by james121285
                      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
                      Code:
                      #include <stdio.h>
                      #include <math.h>
                      
                      int main (void)
                      
                      {
                      	double part1=1, part2, part3, pow3=1, numerator;
                      	int k, n;   // ** changed n to an int to suit the scanf() and for()
                      
                      	printf ("Please input number of iterations");
                      	scanf("%d",&n);
                      
                      	for (k = 1; k < n; k++)//; removed ;
                      	{
                      		if (k%2)                  // ** added 2
                      			numerator = -1.0;      // ** odd
                      		else numerator =1.0;       // ** even
                              pow3=  pow3*3;            // ** added
                      		part1 += numerator / (pow3* ((2* k) +1));
                       		printf ("%f, %d\n", pow3, ((2*k)+1));
                            }
                      		part2 = 3.464101615 * part1;
                      
                      		part3 = M_PI - part2;
                      
                      		printf ("%f, %f", part2, part3);
                      getchar();getchar();
                      	// remove }
                      	return 0;
                      }

                      Comment

                      • james121285
                        New Member
                        • Feb 2007
                        • 18

                        #12
                        I have just tried it. It works perfectly, thanks. Your a star horace1.

                        Thanks again.

                        James

                        Comment

                        • ashith
                          New Member
                          • Feb 2007
                          • 8

                          #13
                          Nice program

                          Comment

                          Working...