Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Approximating One num erica! algorithm to compute an approximation to the consta

ID: 3877032 • Letter: A

Question

Approximating One num erica! algorithm to compute an approximation to the constant involves computing the value of the following sum. N-1 N) (14() where the value N controls the number of steps (and larger values will result in better approxima- tions). Recall from the labs that a numerical summation like the one above can be implemented with a C for or while loop (using extra variables inside the loop to break down the formula into manageable parts). 2.1 Your Task Write a syntactically and semantically correct C program which uses'the formula above to compute several different approximations of using the formula above for N = 10,100 1000 10000 100000 and 1000000. Notice that at each step, the value of N is multiplied by 10. Your code should be stored in a file called "pi.c". To receive full marks, your code should contain as little duplication as possible (and, in particular, should not contain more than one copy of the loop which evaluates the formula above). You will likely need to use nested loops to accomplish this task. 3

Explanation / Answer

#include <stdio.h>

int main()

{
long N = 10;
double pi;
int i;
double t, denominator;
  
printf("%10s %20s ", "N", "PI Approximation");
  
for(N = 10; N <= 1000000L; N *= 10)
{
pi = 0;
for(i = 0; i < N; i++)
{
t = (2 * i + 1.0) / (2 * N);
denominator = 1 + t * t;
pi += 1/denominator;
}
  
pi = 4.0/N * pi;
  
printf("%10ld %20.15lf ", N, pi);
  
}
return 0;
}


output
N PI Approximation
10 3.142425985001099
100 3.141600986923125
1000 3.141592736923123
10000 3.141592654423134
100000 3.141592653598162
1000000 3.141592653589764

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote