Hello, I am currently writing a program that estimates Pi values using three different formulas pictured here: http://i.imgur.com/LkSdzXm.png .

This is my program so far:

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()

{
  double leibniz = 0.0; // pi value calculated from Leibniz

  double counter = 0.0; // starting value

  double eulerall = 0.0; // pi value calculated from Euler (all integers)

  double eulerodd = 0.0; // value calculated from Euler (odds)

  int terms;
  bool negatives = false;

  cin >> terms;
  cout << fixed << setprecision(12); // set digits after decimal to 12

  while(terms > counter){
    leibniz = 4*(pow(-1, counter)) / (2*counter+1) + leibniz;
    counter++;
    eulerall = sqrt(6/pow(counter+1,2)) + eulerall;
    counter++;
    eulerodd = (sqrt(32)*pow(-1, counter)) / (2*counter + 1) + eulerodd;
    counter++;

    cout << terms << " " << leibniz << " " << eulerall << " " << eulerodd <<endl;
  }

  if (terms < 0){
    if(!negatives)
      negatives=true;
    cout << "There were " << negatives << " negative values read" << endl;
  }
  return 0;
}

The sample input file that I am using is:
1
6
-5
100
-1000000
0

And the sample output for this input file is:

     1 4.000000000000 2.449489742783 3.174802103936
     6 2.976046176046 2.991376494748 3.141291949057
   100 3.131592903559 3.132076531809 3.141592586052

When I run my program all I get as an output is:

1 4.000000000000 1.224744871392 1.131370849898.

So as you can see my first problem is that the second and third of my equations are wrong and I can't figure out why. My second problem is that the program only reads the first input value and stops there. I was hoping you guys could help me figure this out. Help is greatly appreciated.

In your loop you are doing counter++; three times. Once is sufficient;
Also I would do the square root, after the loop.

Try this

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
    double leibniz = 0.0; // pi value calculated from Leibniz
    double eulerall = 0.0; // pi value calculated from Euler (all integers)
    double eulerodd = 0.0; // value calculated from Euler (odds)

    double counter = 0.0; // starting value

    int iter;

    cin >> iter;
    if (iter <=0 || iter >= 1e6 ) {
        cout << "Invalid input, default precision will be set to 100 iterations" << endl;
        iter = 100;
    }

    int i = 1;
    while(counter <= iter){
        leibniz = 4*(pow(-1, counter)) / (2*counter+1) + leibniz;
        eulerall += 6/pow(counter+1,2);
        eulerodd += 1/pow(i,3);
        i += 2;
        counter++;
    }
    eulerall = sqrt(eulerall);
    eulerodd = pow(eulerodd*32,0.3333);

    cout << "- Leibniz: " << setprecision(25) << leibniz << endl;
    cout << "- Euler 1: " << setprecision(25) << eulerall << endl;
    cout << "- Euler 2: " << setprecision(25) << eulerodd << endl;

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.