Question
Write a C program that calculates the approximate value of from the following three infinite series: = 4 4/3 +4/5 - 4/7 + 4/9 -
Write a C program that calculates the approximate value of from the following three infinite series:
= 4 4/3 +4/5 - 4/7 + 4/9 - 4/11 + ...
^2 = 12 [1 - 1/4 + 1/9 - 1/16 + 1/25 - 1/36 + ...]
= 12^1/2[1 - 1/( 3 * 3) + 1/(5 * 3^2) - 1/(7 * 3^3) + ...]
A sample interaction and output of the program is given on a separate page. Your program should prompt the user for a non-negative integer and check its validity. If the input is a negative integer, the program should display a message and prompt the user to input a non-negative integer again. All inputs are assumed to be of type integer. If the non-negative integer is zero, the program should terminate, otherwise it should output an approximate value of using the given series and prompt the user again. When writing the program, do not use the formula for the given series, simply write code that will alternately add or subtract a term to/from the series.
After completing and testing your program, answer the following questions and write your answer as comment at the end of your program.
Questions: (a) How many terms of the above series do you have to use before you first get 3.14?, 3.141?, 3.1415?, or 3.14159? Make a table when answering this question. Each row should correspond to each given series and each column should correspond to the above given approximation. (b) Which series converges faster? Briefly explain.
My teacher gave an outline to try and follow, I guess it is pretty much our psuedocode, though I am not sure how complete it is.
Things to keep in mind about program 4: - includewhen you are using any math library function - when compiling a program that uses any math function, you should use "-lm" option of the compiler. For example: gcc pi.c -lm - except for the number of terms that should be an integer, declare all other variables as "long double" - write separate functions for approximating pi according to the given series - declare these functions like: long double approx1(int terms); long double approx2(int terms); long double approx3(int terms); - in the main function: declare an integer variable for the number of terms prompt the user get the input (number of terms) while( terms != 0 ) if(terms == 1) call the first function for displaying pi approximation for one term similarly, repeat the above for the other two functions else if( terms > 1 ) call the first function for displaying pi approximation for more than one term similarly, repeat the above for the other two functions else display wrong input message prompt the user get the next input endwhile display the termination message end main - write 3 functions for approximating pi. For example: long double approx1(int terms) { declare variables pi, num, and denom as "long double" and initialize them declare i // a counter for the for loop for ( i = 1; i <= terms; i = i + 1 ){ if ( i % 2 != 0 ){ pi = pi + num / denom; } else { pi = pi - num / denom; } denom = denom + 2.0; } // end of for loop return pi; } // end of function approx1
Sample Output
Enter the number of terms to approximate pi, or 0 to terminate: -2 Wrong input, please try again Enter the number of terms to approximate pi, or 0 to terminate: 1 First approximation of pi to 1 term is 4.00000000 Second approximation of pi to 1 term is 3.46410162 Third approximation of pi to 1 term is 3.46410162 Enter the number of terms to approximate pi, or 0 to terminate: 10 First approximation of pi to 10 terms is 3.04183962 Second approximation of pi to 10 terms is 3.13297720 Third approximation of pi to 10 terms is 3.14159051 Enter the number of terms to approximate pi, or 0 to terminate: 100 First approximation of pi to 100 terms is 3.13159290 Second approximation of pi to 100 terms is 3.14149811 Third approximation of pi to 100 terms is 3.14159265 Enter the number of terms to approximate pi, or 0 to terminate: 1000 First approximation of pi to 1000 terms is 3.14059265 Second approximation of pi to 1000 terms is 3.14159170 Third approximation of pi to 1000 terms is 3.14159265 Enter the number of terms to approximate pi, or 0 to terminate: 10000 First approximation of pi to 10000 terms is 3.14149265 Second approximation of pi to 10000 terms is 3.14159264 Third approximation of pi to 10000 terms is 3.14159265 Enter the number of terms to approximate pi, or 0 to terminate: 0 ***** Program Terminated *****
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