Page 1 of 1

This type of problem requires you to: Compare the absolute value of the difference of two successive terms. If the value

Posted: Fri Jul 08, 2022 7:27 am
by answerhappygod
This Type Of Problem Requires You To Compare The Absolute Value Of The Difference Of Two Successive Terms If The Value 1
This Type Of Problem Requires You To Compare The Absolute Value Of The Difference Of Two Successive Terms If The Value 1 (78.68 KiB) Viewed 48 times
This type of problem requires you to: Compare the absolute value of the difference of two successive terms. If the value is less that the threshold or margin of error, then the last new value generated is the approximation to the actual value. In other words, • • • Analysis while (Xnew -Xold | > some margin of error) { } sum the new value to previous amount save the new as old. generate the new value, Xnew
. 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;