# Thread: Calculated values coming out strange

1. ## Calculated values coming out strange

Hey guys, I cant see whats going wrong here, values for theta calculate out to a very strange value. Function is suppose to estimate pi by doing sqrt(6*(1/1*1)+1/(2*2)+1/(3*3) etc using 5 and 10 terms. Please help if possible! Thanks alot.

Sam

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double theta(int terms);
const double piValue = 3.141592653589793;

int main (void)
{
int terms;

printf("The value of pi is %lf\n", piValue);
terms=5;
printf("When using %d terms:\n", terms);
printf("theta = %lf\n", theta(terms));
printf("The difference between theta and pi is %lf\n\n",
theta(terms) - piValue);
terms=10;
printf("When using %d terms:\n", terms);
printf("theta = %lf\n", theta(terms));
printf("The difference between theta and pi is %lf\n\n",
theta(terms) - piValue);
system("pause");
return 0;
}//main

double theta(int terms)
{
int denom;
double sumOfTerms;

for(denom = 1; denom <= terms; denom++)
{
sumOfTerms += (double)1/(denom*denom);
}
return sqrt (6*sumOfTerms);
}```

2. You're not initializing sumOfTerms so it's just starting off with some garbage value. Change the line to:
Code:
`double sumOfTerms = 0;`

3. You need to initialize sumofterms to 0. in your theta function.

Code:
```double theta(int terms)
{
int denom;
double sumOfTerms = 0;```

4. that did it. Thanks a million guys!

Sam

5. Turn on your compiler warnings
and start paying attention to them
and you'll be able to save million thanks =)

6. You might want to save the value returned by theta(terms) so that you don't call it twice. For a lot of terms this could save a lot of recalculation.