Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

THE CODING IS ALMOST ALREADY DONE. I have this exercise/challenge that Im doing and I am having some trouble figuring it out. it is almost

THE CODING IS ALMOST ALREADY DONE. I have this exercise/challenge that Im doing and I am having some trouble figuring it out. it is almost done and just needs to be filled in.

Instructions: a similar problem has been answer in this link:

https://www.chegg.com/homework-help/questions-and-answers/approximate-value-pi-calculated-using-series-given-4-1-1-3-1-5-1-7-1-9--1-2n-1-write-c-pro-q52322318

Here is the instructions written and the code that needs to be filled in. Thank you for your help.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

An approximate value of pi can be calculated using the series given below: 4 [ 1 - 1/3 + 1/5 - 1/7 + 1/9 + ... + ((-1)")/(2n + 1) ] Write a C++ program to calculate the value of Pi using this series in two distinct ways, through n iterations and approximation on n significant digits (within a change of 0.X1 decimal value, where X represents a certain amount of zeros). For example, 5 iterations would be to run the above equation for n = 0, 1, 2, 3, and 4. For the approximation within a change of 0.X1 decimal places means that the value of Pi doesn't change for more than 0.X1. For example, let say we want an approximation of 7 significant digits. That means that we will stop our loop iteration of calculating Pi (using the above equation) when the value of Pi from one iteration to the next only changes by 1 x 10 (0.0000001). So how will the program be structured now that we understand the objective? Below is a detailed description of each function. Main should display a message saying this program calculates the number Pi. After your program will prompt the user if they want to calculate Pi through iteration or approximation through the characters 'l' and 'A'. Your program should adjust if the user inputs in lower/upper case characters. Also, if the user inputs something other than I' or 'A', it will make a loop until the user gets it right. Depending on what method the user decides to calculate Pi, you'll prompt for the number of iterations or decimal value of approximation and call the respective function to calculate Pi. Iterate will take as argument the number of items to iterate through the equation calculating Pi. It will have no return value. Once it calculates Pi, it will call the function DisplayPi passing the digit significance of 5 (always) and the value of Pi it calculated to display on the screen. DecimalApprox will take as argument the digit of significance needed to approximate Pi to the decimal value. It will have no return value. You'll do an infinite loop of the Pi equation until the new value and old value of Pi only differ by 1 x 10" (0.X1). You can stop an infinite loop by using a break statement. Once you calculated the Pi value through approximation, you'll call the DisplayPi function passing the number digit significance the user entered (n) and the Pi value calculated. Display Pi will take as argument the significant digit and the value of Pi. It will return nothing. Make sure to set the environment to set the appropriate significant figures to display the calculated Pi to your monitor. Sample Runs #1 (Notice the 7 significant digits in the output that goes with the user input): $ ./PiCalculation This program calculates the number Pi. Would you like to calculate it through I>teration or A>pproximation: a How many decimals of approximation?: 7 The value of Pi is: 3.1415927 #2 (Always 5 significant digits in the display output): $ ./PiCalculation This program calculates the number Pi. Would you like to calculate it through I>teration or A>pproximation: I How many iterations?: 250 The value of Pi is: 3.13759 #3: $ /PiCalculation This program calculates the number Pi. Would you like to calculate it through I>teration or A>pproximation: x Incorrect Selection. Try again... Would you like to calculate it through I>teration or A>pproximation: y Incorrect Selection. Try again... Would you like to calculate it through I>teration or A>pproximation: z Incorrect Selection. Try again... Would you like to calculate it through I>teration or A>pproximation: A How many decimals of approximation?: 3 The value of Pi is: 3.142 15 // ====== =================================================== 16 17 #include teration or "; 42 cout pproximation: "; 43 cin >> ??? 44 45 ??? 46 47 // If it is iteration, ask how many iterations and call the 48 // Iterate function. 49 ??? 50 // Prompt the user of how many iterations 51 cout > ??? 53 54 // Call the Iterate function 55 Iterate(???); 56 57 // If it is approximation, ask the number of approximation and call 58 // the DecimalApprox function 59 60 // Prompt the user of decimal approximation 61 cout > ??? 63 64 // Call the DecimalApprox function 65 DecimalApprox (???); 66 } 67 return 0 68 } // end of "main" 69 70 71 70 71 72 // Iterate 73 // 74 // This function calcuates pi through iteration 75 // 76 // Input: 77 // iter [IN] an integer value containing the number of iterations 78 // 79 // Output: 80 // The value of pi. 81 // 82 // 83 84 void Iterate(int iter) 85 { 86 ??? // variables 87 88 // do a loop to calculate pi based on the number of iterations 89 ??? 90 { 91 ??? 92 } 93 94 // Call the DisplayPi function. 95 // default to 5 digit significance for iteration 96 DisplayPi(5, ???); 97 98 } // end of "Iterate" 99 100 101 102 // ==== DecimalApprox 103 // 104 // This function calcuates pi through decimal approximation 105 // 106 // Input: 107 // approx [IN] -- an integer value containing the approximation 108 // 109 // Output: 110 // The value of pi. 111 // 112 // 113 114 void DecimalApprox(int approx) 115 { 116 ??? // variables 117 double pival ??? // Pi value 118 double previousPi pival // Previous pi value 119 120 // loop to calculate pi based on the approx value of signif. digits. 121 do{ 122 ??? // calculate PI 123 124 125 126 127 // check if the significance has changed within 1 decimial approx. // if so break out of the loop if (abs (pival - previousPi) teration or A>pproximation: a How many decimals of approximation?: 7 The value of Pi is: 3.1415927 #2 (Always 5 significant digits in the display output): $ ./PiCalculation This program calculates the number Pi. Would you like to calculate it through I>teration or A>pproximation: I How many iterations?: 250 The value of Pi is: 3.13759 #3: $ /PiCalculation This program calculates the number Pi. Would you like to calculate it through I>teration or A>pproximation: x Incorrect Selection. Try again... Would you like to calculate it through I>teration or A>pproximation: y Incorrect Selection. Try again... Would you like to calculate it through I>teration or A>pproximation: z Incorrect Selection. Try again... Would you like to calculate it through I>teration or A>pproximation: A How many decimals of approximation?: 3 The value of Pi is: 3.142 15 // ====== =================================================== 16 17 #include teration or "; 42 cout pproximation: "; 43 cin >> ??? 44 45 ??? 46 47 // If it is iteration, ask how many iterations and call the 48 // Iterate function. 49 ??? 50 // Prompt the user of how many iterations 51 cout > ??? 53 54 // Call the Iterate function 55 Iterate(???); 56 57 // If it is approximation, ask the number of approximation and call 58 // the DecimalApprox function 59 60 // Prompt the user of decimal approximation 61 cout > ??? 63 64 // Call the DecimalApprox function 65 DecimalApprox (???); 66 } 67 return 0 68 } // end of "main" 69 70 71 70 71 72 // Iterate 73 // 74 // This function calcuates pi through iteration 75 // 76 // Input: 77 // iter [IN] an integer value containing the number of iterations 78 // 79 // Output: 80 // The value of pi. 81 // 82 // 83 84 void Iterate(int iter) 85 { 86 ??? // variables 87 88 // do a loop to calculate pi based on the number of iterations 89 ??? 90 { 91 ??? 92 } 93 94 // Call the DisplayPi function. 95 // default to 5 digit significance for iteration 96 DisplayPi(5, ???); 97 98 } // end of "Iterate" 99 100 101 102 // ==== DecimalApprox 103 // 104 // This function calcuates pi through decimal approximation 105 // 106 // Input: 107 // approx [IN] -- an integer value containing the approximation 108 // 109 // Output: 110 // The value of pi. 111 // 112 // 113 114 void DecimalApprox(int approx) 115 { 116 ??? // variables 117 double pival ??? // Pi value 118 double previousPi pival // Previous pi value 119 120 // loop to calculate pi based on the approx value of signif. digits. 121 do{ 122 ??? // calculate PI 123 124 125 126 127 // check if the significance has changed within 1 decimial approx. // if so break out of the loop if (abs (pival - previousPi)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions