Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
How do I compute pi in c++ withing 10^-5 of actual pi using u=2 and v= 1/sqrt(2) and replacing u with u/v and v with sqrt((1+v)/2) each iteration. I have no programming background! This is all I got:


C++
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
  float u,v;

  u=2;
  v=1/sqrt(2);



PLEASE HELP!!!

It is due tomorrow
Posted
Updated 28-Jan-14 6:52am
v5
Comments
Richard MacCutchan 28-Jan-14 12:53pm    
What is due, and since you have no programming background why are you being asked for this?
[no name] 28-Jan-14 13:13pm    
Here's another hint: Taylor Series.
MalDrHoop 28-Jan-14 14:15pm    
I know its a Taylor series. I just don't know what to do. I don't know programming

Your code shown is incomplete.
But that is beside the point...
Your homework is given to help you to learn.
If we give you a solution you will not have the "think through" solving the problem.

I'll give you a HINT:
type double will give more precision than float

EDIT:
Since you imply this is not for a c++ programming course, but that you were expected to have some programming background, here is the iteration part of the program.
C++
#include <iostream>
#include <math.h>

using namespace std;
 
int main()
{
  float u,v, prevu;
 
  u=2;
  v=1/sqrt(2);
  prevu = 0;
  while (fabs(prevu - u) > 10e-6f)
  {
    prevu = u;
    u = u/v;
    v = sqrt((1 + v)/2);
  }
  // Here u will contain the approximation to pi
  // Here is where the result should be output
}

My c++ I/O is very rusty, sorry.
 
Share this answer
 
v3
Comments
MalDrHoop 28-Jan-14 14:15pm    
I was told to use float. I have NO programming background. I know the code is incomplete but I have no idea what I am doing therefore your hint does not help me. I was not taught ANY c++ and do not have hardly any time to teach myself.
Matt T Heffron 28-Jan-14 14:20pm    
OK, then you are already pretty close to the limit of precision that is available with float.
If you use the "Improve question" and show all of what you are doing we may be able to help you "fine tune" it.
(And like Richard MacCutchan noted: If you have no programming background, why are you taking a course which seems to assume c++ as a pre-requisite?)
MalDrHoop 28-Jan-14 14:31pm    
I've shown all I've done. Except for some reason it removed the <iostream> and <math.h> after the #include (s) respectively when I submitted. I've given all the instructor supplied. They placed me in the course because it is the only time I can take it and it is a requirement.
Matt T Heffron 28-Jan-14 15:05pm    
Can you show the code that you have that follows what is above?
All we can see is the initial assignments to u and v.
What is your iteration code?
MalDrHoop 28-Jan-14 15:15pm    
My question preceding the program is all that was given to me in the class.
Here is what I finally came up with. Definitely not the best way by no means but best I can do with my very limited knowledge.

C++
#include < iostream>
#include < math.h> //so that sqrt function may be used
 
using namespace std;
 
int main()
{
float u,v, prevu;
 
u=2;
v=1/sqrt(2);
prevu = 0;
 
while (fabs(prevu-u) > 10e-6f) //fabs: floating point absolute value function. f: means it's a type of float
{
prevu = u;
u=u/v;
v=sqrt((1+v)/2);
}
cout << " pi ~ " << u << endl;
}
 
Share this answer
 
v5
Comments
Matt T Heffron 29-Jan-14 12:39pm    
At the end of the loop u should contain the approximation to π (it does for me).
The calculation in the cout line should be unnecessary.
MalDrHoop 29-Jan-14 12:45pm    
I have a better solution now. I will submit it in a moment.
Apparently I forgot to mention if didn't matter which code the program was in at the beginning when I entered my question (new to site remember) but I prefer c or c++ over the others cause of the small amount of stuff I've looked at and understood it was in c++ and c++ and c is what I will be taking next semester and what some people have tried to explain to me.


C#
#include <math.h>               // to use square root function
#include <stdio.h>              // to use print

int main()
{
    float u = 2.0;
    float v = 1/ sqrt((float)2.0);

    printf("Pi approximation\n\n");  //prints output

    for (int i = 0; i < 12; i++)
    {
        u = u/v;
        v = sqrt((1 + v) / 2);
        printf("%f\n", u);      //prints u for each loop onto screen
    }

    getchar();
    return 0;
}
 
Share this answer
 
v2
Comments
Matt T Heffron 29-Jan-14 13:11pm    
Now, I'm going to put on my "former TA" hat, if you don't mind and ask a couple of questions for you to think about (no answer here necessary)
Why 12 iterations? Why not 5, 10, 100?
Is that the optimal number of iterations to get the desired precision?
Numerical Analysis includes not doing unnecessary computation as well.
MalDrHoop 29-Jan-14 13:27pm    
Your deduction is correct, 12 iterations was all that was necessary to get the desired outcome.
Matt T Heffron 29-Jan-14 14:05pm    
Are you sure it is 12 and not 11?
I modified mine to output the fabs(prevu - u) and that value was ~9.5e-7 after the 11th iteration.
MalDrHoop 29-Jan-14 13:26pm    
This works perfectly and someone gave me one star? Wow I guess I should just quit on here.
Matt T Heffron 29-Jan-14 13:59pm    
I think the one star is because NORMALLY you shouldn't post SOLUTIONS to your own questions.
Especially, multiple Solutions.
(It looks like abuse of the Reputation Points system, because Solutions are worth more.)
In the future, the recommended way is to use the "Improve question" (above, at the bottom right of your original question) and add them to the end of the question with comments like "This is my latest attempt", or "Thanks, this is what I ended up with".
its quite simple
double dPI = 2*(asin(1));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900