This type of problem requires you to: Compare the absolute value of the difference of two successive terms. If the value
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
This type of problem requires you to: Compare the absolute value of the difference of two successive terms. If the value
. Analysis In this example let the margin of error between two successive values be 0.05. In addition, each new term may be generated as follows expression: pow(-1.0, n)/(2* n + 1);
1. 2. ก่ VON 9. 10. 11. 12. 13. 14. 15. 16. Sketch of the program 1* Define any constants. For instance MARGIN_OF_ERROR = 0.05 */ // Include any necessary file. For instance include stdio.h /* Declare any necessary variables for instance double sum; int iterate; *1 /* */ Initialize the variable sum. That is sum = 1; /* You may want to define a function that finds each new term. For example public double findXnew(int n) { */ } return pow(-1.0, n)/(2* n + 1);
1. LOCAWNY 2. 7. } See example on next slide Analysis (cont) Your code details the while loop construct // Details to come... while (abs(xnew - xold) > MARGIN_OF_ERROR) { // Complete........
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. Example void findPi() { 16. 17. } int i = 1; double xold = 2.0; // Choose a value double xnew = findXnew(i); while (abs(xnew - xold) > MARGIN_OF_ERROR) { } sum = sum + xnew; xold = xnew; i++; xnew = findXnew(i); iterate = i;