Relevant Programming Concepts Control Structure User defined

Relevant Programming Concepts: Control Structure User defined function File IO The value of pi pi, can be approximated using the Gauss-Legendre algorithm, which iteratively improves a precision of desired accuracy as Initial values: a_0 = 1, b_0 = 1/Squareroot 2, t_0 = 1/4, p_0 = 1 Repeat the following instructions n times to get desired accuracy:a_n + 1 = a_n + b_n/2 t_n + 1 = t_n - p_n (a_n + 1 - a_n)^2 b_n + 1 = Squareroot a_n b_n p_n + 1 = 2p_n pi is then approximated as pi = (a_n + 1 + b_n + 1)^2/4t_n + 1 Write a C function that calculates the value of pi with a variable level of accuracy. For instance, if the desired accuracy is 10^-4, the absolute difference between the true value of pi (see below) and the approximation from your function is less than 10^-4 Function prototype: double Pi_value(int n); ASSERT your function by running unit tests (see see 5.7 in your Zybook) for values 3, 4 and 6 Examples of testing your function are const double pi = 3.141592653589793238462643383; assert(fabs(Pi_value(3) - pi)

Solution

a)

this is the c function prototype for approx pi value using gauss langender algorithm.

store the return of function where ever you call it and then using assertion statement in question get the answer. before all this assertion declares the const double value of pi.

Double Pi_value(int n){
double a=0,b=(1/sqt(2)),t=0.25,p=1;
double ao;
double piv;
while(n){
n--;
ao=a;
a=(a+b)/2;
b=sqrt(ao*b);
t=t-p*pow((a-ao),2);
p=2*p;
piv=pow((a+b),2)/(4*t);
}
return piv;
}

b) For determing the area of the region we need pi value . if our asseration is true then in next step will use function to return the area. radius and angle theta is passed along with pi_value to calculate the area.

double calculate_area(double pi_v, int r, int theta){
return(pi_v*r*r*(theta/60));
  
}

 Relevant Programming Concepts: Control Structure User defined function File IO The value of pi pi, can be approximated using the Gauss-Legendre algorithm, whic

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site