Velocity Reviews > C++ > Computing pi to nth terms?

# Computing pi to nth terms?

Kraig
Guest
Posts: n/a

 09-18-2004
Hi! I'm new to programming and am trying to figure out the best way
using loops, to compute pi to say, 14 terms using values that double
each time through the loop. As in, from 1-2-4-8-16, et al. I'm at a
loss as to where to start. Does anyone have any ideas how to do this?

kraig

John Harrison
Guest
Posts: n/a

 09-18-2004

"Kraig" <(E-Mail Removed)> wrote in message
news:zmM2d.173745\$%(E-Mail Removed) ...
> Hi! I'm new to programming and am trying to figure out the best way using
> loops, to compute pi to say, 14 terms using values that double each time
> through the loop. As in, from 1-2-4-8-16, et al. I'm at a loss as to
> where to start. Does anyone have any ideas how to do this? Thanks for any
>
> kraig

What are you having trouble with, the formula, or how to program it?

If you are having trouble with the formula then that's a math question, try
in sci.math.

If its the programming then why not repeat the formula for those who are
less good a math then you. This will increase you chances of getting an

Note that this group does not do homework unless you have made some effort
yourself, so show us what you have done so far.

If you really are at a loss where to start and haven't programmed a single
line of code yet then I suggest that you start on a simpler task. Forget
about pi and write a program that uses a loop to output the terms 1 2 4 8 16
32 etc. 14 numbers in all. If you can't manage that then maybe programming
is not for you.

john

osmium
Guest
Posts: n/a

 09-18-2004
"Kraig" write:

> > Hi! I'm new to programming and am trying to figure out the best way

using
> > loops, to compute pi to say, 14 terms using values that double each time
> > through the loop. As in, from 1-2-4-8-16, et al. I'm at a loss as to
> > where to start. Does anyone have any ideas how to do this? Thanks for

any

Any time you can provide an explicit number, such as 14 above, it is an
indicator that the solution will most likely include a for statement in some
fashion.

George
Guest
Posts: n/a

 09-18-2004

"Kraig" <(E-Mail Removed)> wrote in message
news:zmM2d.173745\$%(E-Mail Removed) ...
> Hi! I'm new to programming and am trying to figure out the best way using
> loops, to compute pi to say, 14 terms using values that double each time
> through the loop. As in, from 1-2-4-8-16, et al. I'm at a loss as to
> where to start. Does anyone have any ideas how to do this? Thanks for any
>
> kraig

#define PI 3.14159

double total = 0;
unsigned char i;

for(i=0; i<14; i++)
total *= PI;

George
Guest
Posts: n/a

 09-18-2004

"George" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Kraig" <(E-Mail Removed)> wrote in message
> news:zmM2d.173745\$%(E-Mail Removed) ...
>> Hi! I'm new to programming and am trying to figure out the best way
>> using loops, to compute pi to say, 14 terms using values that double each
>> time through the loop. As in, from 1-2-4-8-16, et al. I'm at a loss as
>> to where to start. Does anyone have any ideas how to do this? Thanks for
>>
>> kraig

>
> #define PI 3.14159
>
> double total = 0;
> unsigned char i;
>
> for(i=0; i<14; i++)
> total *= PI;

but then, that would result in total == 0.

maybe double total = PI:

you will have to finish the assignment yourself sir.

regards

Kraig
Guest
Posts: n/a

 09-19-2004
That's where I got lost, on the #define part. I had, to no avail:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
float fCalcPI = 0;
float fTerms = 0;

cout << "Enter number of terms.\n";
cin >> fTerms;

fCalcPI = fTerms - (fTerms/3) + (fTerms/5) - (fTerms/7) + (fTerms/9) -
(fTerms/11);

cout << "Pi is " << fCalcPI << " \n";

return 0;
}

Like I said, I'm learning as I go. Please be patient

kraig

George wrote:
> "George" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>"Kraig" <(E-Mail Removed)> wrote in message
>>news:zmM2d.173745\$%(E-Mail Removed) et...
>>
>>>Hi! I'm new to programming and am trying to figure out the best way
>>>using loops, to compute pi to say, 14 terms using values that double each
>>>time through the loop. As in, from 1-2-4-8-16, et al. I'm at a loss as
>>>to where to start. Does anyone have any ideas how to do this? Thanks for
>>>
>>>kraig

>>
>>#define PI 3.14159
>>
>>double total = 0;
>>unsigned char i;
>>
>>for(i=0; i<14; i++)
>> total *= PI;

>
>
> but then, that would result in total == 0.
>
> maybe double total = PI:
>
> you will have to finish the assignment yourself sir.
>
> regards
>
>

John Harrison
Guest
Posts: n/a

 09-19-2004

"Kraig" <(E-Mail Removed)> wrote in message
news:Hgj3d.178780\$%(E-Mail Removed) ...
> That's where I got lost, on the #define part. I had, to no avail:
>
> #include <iostream>
> #include <cmath>
> using namespace std;
>
> int main()
> {
> float fCalcPI = 0;
> float fTerms = 0;
>
> cout << "Enter number of terms.\n";
> cin >> fTerms;
>
> fCalcPI = fTerms - (fTerms/3) + (fTerms/5) - (fTerms/7) + (fTerms/9) -
> (fTerms/11);
>
> cout << "Pi is " << fCalcPI << " \n";
>
> return 0;
> }
>
> Like I said, I'm learning as I go. Please be patient
>
> kraig

Well I can see a few problems here. Firstly you are not using a loop, but
the problem you have to solve is 'the best way using loops, to compute pi to
say, 14 terms using values that double each time through the loop'. Clearly
you must use a loop somewhere.

Secondly you code does nothing like calculate PI. I still don't know what
the formula is that you are trying to use (if I did I would be able to help
you better). But look at what you wrote

cout << "Enter number of terms.\n";
cin >> fTerms;
fCalcPI = fTerms - (fTerms/3) + (fTerms/5) - (fTerms/7) + (fTerms/9) -
(fTerms/11);

Suppose I entered 14 as the number of terms, then the calculation would be

fCalcPI = 14 - (14/3) + (14/5) - (14/7) + (14/9) - (14/11);

which is nothing at all like the correct value of PI.

For the number of terms you want something like this

cout << "Enter number of terms.\n";
int fTerms;
cin >> fTerms;
for (int i = 0; i < fTerms; ++i)
{
...
}

See? That is a loop, and the program will go round the loop the same number
of times as the number of terms that the user entered. You have to fill in
the ... part (and a bit more). I would imagine (but I don't know this
formula) that you need to sum some series. So each time around the loop you
add another term to a total, something like this

float fTotal = 0;
cout << "Enter number of terms.\n";
int fTerms;
cin >> fTerms;
for (int i = 0; i < fTerms; ++i)
{
fTotal += ...;
}

Again you've still got to fill in the ... part. And I'm guessing here, it
might be that the formula you have in mind is completely different.

Also note that fTerms is an integer. For some reason you have declared it as
a float. But the number of terms must be an integer, you cannot have half a
term!

john

Kraig
Guest
Posts: n/a

 09-19-2004
John,

Perhaps that is part of my problem. I lack an understanding of how to
calculate pi using numbers of terms that are powers of 2 in C++, in the
most effective manner.

I do know I need a loop. However, I left it out because I feel it's
better to calculate pi properly, then move on to looping/incrementing
the terms. I hope that makes sense, John. Thank you for your help - it
is invaluable!

kraig

John Harrison wrote:

> "Kraig" <(E-Mail Removed)> wrote in message
> news:Hgj3d.178780\$%(E-Mail Removed) ...
>
>>That's where I got lost, on the #define part. I had, to no avail:
>>
>>#include <iostream>
>>#include <cmath>
>>using namespace std;
>>
>>int main()
>>{
>>float fCalcPI = 0;
>>float fTerms = 0;
>>
>>cout << "Enter number of terms.\n";
>>cin >> fTerms;
>>
>>fCalcPI = fTerms - (fTerms/3) + (fTerms/5) - (fTerms/7) + (fTerms/9) -
>>(fTerms/11);
>>
>>cout << "Pi is " << fCalcPI << " \n";
>>
>>return 0;
>>}
>>
>>Like I said, I'm learning as I go. Please be patient
>>
>>kraig

>
>
> Well I can see a few problems here. Firstly you are not using a loop, but
> the problem you have to solve is 'the best way using loops, to compute pi to
> say, 14 terms using values that double each time through the loop'. Clearly
> you must use a loop somewhere.
>
> Secondly you code does nothing like calculate PI. I still don't know what
> the formula is that you are trying to use (if I did I would be able to help
> you better). But look at what you wrote
>
> cout << "Enter number of terms.\n";
> cin >> fTerms;
> fCalcPI = fTerms - (fTerms/3) + (fTerms/5) - (fTerms/7) + (fTerms/9) -
> (fTerms/11);
>
> Suppose I entered 14 as the number of terms, then the calculation would be
>
> fCalcPI = 14 - (14/3) + (14/5) - (14/7) + (14/9) - (14/11);
>
> which is nothing at all like the correct value of PI.
>
> For the number of terms you want something like this
>
> cout << "Enter number of terms.\n";
> int fTerms;
> cin >> fTerms;
> for (int i = 0; i < fTerms; ++i)
> {
> ...
> }
>
> See? That is a loop, and the program will go round the loop the same number
> of times as the number of terms that the user entered. You have to fill in
> the ... part (and a bit more). I would imagine (but I don't know this
> formula) that you need to sum some series. So each time around the loop you
> add another term to a total, something like this
>
> float fTotal = 0;
> cout << "Enter number of terms.\n";
> int fTerms;
> cin >> fTerms;
> for (int i = 0; i < fTerms; ++i)
> {
> fTotal += ...;
> }
>
> Again you've still got to fill in the ... part. And I'm guessing here, it
> might be that the formula you have in mind is completely different.
>
> Also note that fTerms is an integer. For some reason you have declared it as
> a float. But the number of terms must be an integer, you cannot have half a
> term!
>
> john
>
>

John Harrison
Guest
Posts: n/a

 09-19-2004
> I do know I need a loop. However, I left it out because I feel it's
> better to calculate pi properly, then move on to looping/incrementing the
> terms.

That's a good approach, break down the problem into smaller parts and
concentrate on one part. I would have chosen to write the loop first and
then concentrate on the terms. But that's just personal preference.

john

osmium
Guest
Posts: n/a

 09-19-2004
Kraig writes:

> Perhaps that is part of my problem. I lack an understanding of how to
> calculate pi using numbers of terms that are powers of 2 in C++, in the
> most effective manner.
>
> I do know I need a loop. However, I left it out because I feel it's
> better to calculate pi properly, then move on to looping/incrementing
> the terms. I hope that makes sense, John. Thank you for your help - it
> is invaluable!

I am not at all sure that there is a series such as the one you conjecture
about. Is it possible you misunderstood the assignment? An assignment such
as "Computer the first n terms of the alternating series for pi" makes
perfect sense for an introductory programming course. The first hit on
google for <series pi> yields the necessary equation. The terms involve an
alternating sign so the instructor probably wants you to be clever in
computing the appropriate sign.

It's a poor way to compute pi, which is fine for an introductory course, you
can put in debugging statements and observe the result improving.