Please plz plz i need help

Pages: 12
I want to write a program to calculate Pi:
Pi=4-4/3+4/5-4/7+4/9-4/11+...
(the user inputs the number of terms in the series)
plz how should I do ?
Use a loop (such as a for loop) to add up the sum of the terms of the series.

There's a simple pattern, the sign alternates, so you could do
double numerator = 4.0;
double denominator = 1.0;
double total = 0;

Then find the first term, numerator / denominator and add it to the total.
after that, numerator = - numerator
and denominator = denominator + 2
Repeat.

The series is infinite, so finding the exact result is impossible. You just have to choose some fixed number of terms.
I'm beginner in C++
so can you simplify your lang
can you write in c++? plz
The idea is that you should attempt this for yourself first. Then post here the code which you have written, and we can see where the problems may be.
#include<iostream>
using namespace std;
int main ()
{
double n=4.0;
double d=1.0;
double pi;
int i=1;
cout<<"Enter the number of terms in the series";
cin>>n>>d;
while(d=1)
{
d=d+2;
n=-n;
pi=n/d;
cout<<pi<<endl;
}
system("pause");
return 0;
}
it need some changes i try it but it gave me infinit loop
closed account (3qX21hU5)
First please use codetags when posting your code to the forums (Highlight all the code you pasted and click the <> under format:)


1) while(d=1) This is not doing what you think it is suppose to do. "=" is the assignment operator, meaning it assigns stuff to a variable. So what you are are saying in your while condition is make d hold 1.

What you are looking to do is this while (d == 1). Though that logic is wrong also because your while loop will only execute IF and only IF the user enters '1' for d in cin>>n>>d;. Also even if they enter 1 it will only go through your loop once since you add 2 to d in your first statement in the loop d=d+2;. So I don't think you want to do that.

But I'll leave the program logic to you ;p

2)
int i = 1;
You declare i but you never use it...


That takes care of all the errors and warning for your program. You still have some logic to solve but I will leave that to you to figure out since that is the fun part in learning programming ;p.

If you do need help with how to structure this program or help with any of the logic in it, just ask a specific question about it and we will try and help you to the best of our abilities.

BUT we will not do it for you.
Last edited on
It is easy as pie.
Just a few minutes googling.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Pi = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
// http://www.proofwiki.org/wiki/Leibniz%27s_Formula_for_Pi

#include<iostream>
#include<cmath>     // this is for pow()
using namespace std;
int main ()
{
    double numofTerms;
    double pi=0;

    cout<<"Enter the number of terms in the series: ";
    cin>>numofTerms;

    for (double k=0.0; k<=numofTerms; k++) {
        pi += 4.0 * ( pow((-1.0),k) ) * ( 1.0 / (2.0*k + 1) );
        cout<<"k= "<<k<<" ==> pi= "<<pi<<endl;
    }

    //system("pause");
    return 0;
}
It is easy as pie.
I think in this case it's easy as PI.

Just a few minutes googling.
Cheating by stealing other peoples code off the internet is NOT going to help him when he has to take a test or actually program something. Please allow the more experienced posters here to help out the noobs. ;-)

closed account (3qX21hU5)
I don't think he "stole" (Copy and pasted) his code off the internet, but you are right generally it is better to nudge the poster in the right direction so they figure it out on their own and gain more experience instead of just copying the answer.
I have written this code by myself.
I mean on googling that you can read about Leibniz's formula.
You can find a good formula for your code
as I did it on site http://www.proofwiki.org/wiki/Leibniz%27s_Formula_for_Pi

I'm also a beginner in C++.
I try to solve some of the posts I read here
just for learning purposes.
Last edited on
fx11 wrote:
I try to solve some of the posts I read here
just for learning purposes.

By all means do that for your own benefit.

But, if you post the code here in the forum, there may be a cost to others. The person who originally asked the question may not learn anything, except how to copy and paste. Or they may see some code and take it as a model to be emulated and admired, which might be misleading.
Chervil,

Thanks for your suggestions.

My opinion is:
People can learn from code that is written, at least I sometimes do this too.
(And the tutorial of this site has also example codes.)
If somebody want learn, then (s)he will learn.

I don't understand your last sentence.
If a code is working and is doing what it has to,
then this kind of code can be wrong and misleading?
Please explain it! I would like to write good code. How should I do it?
Last edited on
Well, the last part of my comment has several levels.

Now I'm not particularly criticising the code which you posted, I'd much prefer to keep this a more general topic.

There are many possible solutions to a particular problem. And it may be that several completely different methods can all be considered good. I'm not suggesting there is only one single way to write a program.

To begin with there is the choice of algorithm, which is largely independent of the programming language, it's just describing the steps which will be taken to reach a solution. Even at this stage, before a single line of code has been written, some solutions may be "better" than others - though this may be a matter of opinion.

When the code itself is written, there are all sorts of variations, for example the use of c-style strings or file access versus C++ versions of the same functionality. There are other things such as choice of variable names, types of variable (e,g. int versus double), all sorts of things. For example how readable and comprehensible is the code. How easy would it be to modify if the requirements were to change. Is the code easily re-useable? For example, let's say another rather similar problem arises next week, would it be possible to take some existing code and adapt it, or would it be necessary to start again from scratch.

Simply producing code which executes and produces the correct answer is the minimum requirement. For example there may be a bug in the code which hasn't manifested itself so far, such as accessing memory outside the bounds of an array, the program may run and give the correct output, but still contain serious flaws.

Thanks. It's clear now.
Sorry about the confusion fx11, I actually wasn't implying that you "stole" the code. I saw the link was to a wiki page on the math involved (which was cool), not to a page with code.

It's just that once many posters here get to see the solution they will rarely not grab it and move on (human nature). In any case, once they've seen the solution they've lost the opportunity to work it out for themselves and THAT is what programming is really all about.
guys are you fighting each other ?!... anyway all your answer was wrong
you didn't answer my question ...anw thx ...
closed account (3qX21hU5)
Actually their answers were right, so you must have copied them wrong ;p or your using a bad compiler, or some other misc. problems ;p
maybe ! you didnt understand what i wrote in my question carefully:p
@fx11

Sorry to say this, after all the other comments, but I don't like the code.

LIne 15 - it is a bad idea to use doubles in any type of loop, and it is bad to use the ++ operator on anything that is not an integral type. It is also bad to use any kind of equality operator directly on a FP number.

A better approach is to use ints in the for loop and cast them to doubles in the body of the loop.

You might have got a way with it here because the increment value is 1.0, but your code would give unexpected results if it was some other value.

Floating Point (FP) including float & double are stored as binary fractions, and cannot represent every real number - even some of the whole numbers.

@abzksm

Hopefully from my comments you can see how to alter fx11's code to use the Leibniz Formula.

Also fx11 is right in that you should always look on Google or wiki to find answers, & don't forget the reference section at the top left of this page.

HTH
closed account (3qX21hU5)
Or maybe you didn't write what you wanted correctly?

Because your question was this
I want to write a program to calculate Pi:
Pi=4-4/3+4/5-4/7+4/9-4/11+...
(the user inputs the number of terms in the series)
plz how should I do ?


And your luckier then most since someone gave you code the code that you can copy and paste that fits what you said you wanted and if it doesn't it shouldn't be to hard to modify it to do what you wanted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Pi = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
// http://www.proofwiki.org/wiki/Leibniz%27s_Formula_for_Pi

#include<iostream>
#include<cmath>     // this is for pow()
using namespace std;
int main ()
{
    double numofTerms;
    double pi=0;

    cout<<"Enter the number of terms in the series: ";
    cin>>numofTerms;

    for (double k=0.0; k<=numofTerms; k++) {
        pi += 4.0 * ( pow((-1.0),k) ) * ( 1.0 / (2.0*k + 1) );
        cout<<"k= "<<k<<" ==> pi= "<<pi<<endl;
    }

    //system("pause");
    return 0;
}



Enter the number of terms in the series: 5
k= 0 ==> pi= 4
k= 1 ==> pi= 2.66667
k= 2 ==> pi= 3.46667
k= 3 ==> pi= 2.89524
k= 4 ==> pi= 3.33968
k= 5 ==> pi= 2.97605

Process returned 0 (0x0)   execution time : 1.169 s
Press any key to continue.


And now your saying what he gave you was wrong? I'm pretty sure it is the right program for what you asked for, if not maybe you should write it yourself instead of asking for someone else to write it for you. ;)

I'm beginner in C++
so can you simplify your lang
can you write in c++? plz
Last edited on
Pages: 12