Question
What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the
What to do
Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways:
- The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void);
- The second version will break out of a while loop depending on whether a pair of adjacent elements (remember that we always consider a positive element and its adjacent negative right neighbor) is smaller than a certain threshold. I ask you to use the threshold 0.00000000000001. Please put the code of this version into a function with the signature float get_pi_while(void);
Put both functions into a file called pi.c. Also write a corresponding header file that has symbolic constants for both the number of LOOPS to be run as well as for the THRESHOLD value.
Finally, write a file main.c that contains the driver, which will print out pi twice calling the two different functions. I want you to call get_pi_for() first, then call get_pi_while().
What to submit
here is code but it not running correctly plz answer code file with screenshot
- All three code files
- A screen shot of a run of your driver with the exact numbers for LOOPS and THRESHOLD given above
//compile program in following order
//Header file
//pi.h
# define threshold 0.00000000000001 float get_pi_for(); float get_pi_while();
//pi.c
# include
float get_pi_for(){ double pi = 0; unsigned long index = 1; double temp = 0; while (temp - threshold) { temp += ((4.0/index) - (4.0/index+2)); index += 4; } pi = temp; return pi; }
float get_pi_while(){ double pi = 0; unsigned long index = 1; int sign = 1; while (index < 28284277) { pi += 4.0/index * sign; sign *= -1; index += 2; } return pi; }
//main.c
#include"pi.c"
int main(){ printf("Value of pi using for loop is %f ",get_pi_for()); printf("Value of pi using while loop is %f ",get_pi_while()); return ; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started