Você está na página 1de 2

1.

41

//Created by Viet Tran
//Date: 8-28-14

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int n;//n determines how accurate the approximation will be.
int exp;//exp is the exponent to which (-1) is being raised to.
int i;//i is our counter
double Ans;//Ans is our variable we're storing our calculations in
double TruRelErr;//True Relative Error
double PreviousAns = 0;//Previous answer is what we're adding Ans to for summation

//Asking user input for n
printf("\nPlease give a value for n\n");
scanf("%i",&n);
printf("n: %i\n",n);

//Loops the equations for n terms
for(i=1; i<=n; i++){
exp = i-1;
Ans = pow(-1, exp);
Ans = Ans/((2*i)-1);
Ans = PreviousAns + Ans;
PreviousAns = Ans;
}

Ans = Ans*4;//This multiplies the answer by 4 as stated in the equation.
TruRelErr= (M_PI - Ans)/M_PI;//True Relative Error is equal to pi minus answer
//displays the approximation and true relative error
printf("The approximation at n = %i, is %g\n", n, Ans);
printf("True Relative Error is %g\n", TruRelErr);
return 0;
}






Output:

Please give a value for n
10
n: 10
The approximation at n = 10, is 3.04184
True Relative Error is 0.0317524

Please give a value for n
20
n: 20
The approximation at n = 20, is 3.09162
True Relative Error is 0.0159056

Please give a value for n
40
n: 40
The approximation at n = 40, is 3.1166
True Relative Error is 0.0079565

Você também pode gostar