Pi Approximation

I have to write a program which approximate the number pi in two different ways. I get in my IDE the right results. The higher the input the better should be the approximation. I have to hand in my source code at the online IDE from my university. And know the output is like "4.15477e+225" and so one. Really hight numbers. My assumption is that is because of floating points. But I really don't where is my mistake.

I am a beginner. We started programming 3 weeks ago in my university.

Thanks



#include <iostream>
#include <string>
//#include <cstdio>
#include <math.h>
#include "tests.h"

using namespace std;

int main(){

double b;
double x = 0;
double sumspi;
double pi1;
int n;
cin >> n;

while (x < n){ // if x is smaller than n

b = pow(-1, x); // calculate if its negative or positive

sumspi = (b * ( 1/(2*x+1)) * 4); //calculate the parts of the serie
pi1 += sumspi; // sums up the parts of the serie
++x;

}
cout << pi1 << endl;

double z = 3;
double w = 1;
double u = 1;
double o = 1;
double sumspi2;
double pi2;
int s = 1;

if (n==1) {
cout << "2"<< endl;
}
else {

while (s < n){
sumspi2 = (((o*w)/(z*u)));
pi2 += sumspi2;
o = w;
w +=1;
u = z;
z += 2;
++s;
}
}
pi2 +=1;
pi2 = 2 * pi2;
cout << pi2 << endl;

return 0;
}
Hi,

You have 2 variables which are un-initialised, can you figure out which ones? How to avoid this in the future? It's a golden rule of computing :+)

Hope this helps :+)
Topic archived. No new replies allowed.