Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that implements a simple recursive method to calculate the v

ID: 668088 • Letter: W

Question

Write a C++ program that implements a simple recursive method to calculate the value of the number p to a selected degree of precision. (The purpose of this assignment IS NOT to learn how to approximate p; it is to practice writing an iterative code.) The algorithm for this method is as follows:

Step 1: Provide a guess to the value of the number p. This initial guess must be a positive number greater than zero but smaller than 2p (exactly zero or exactly 2p does not work; but anything in the range [0.001 – 6.2831] will.) You can call this initial guess “old_Pi”.

Step 2: Improve the estimate – to be called “new_Pi” -by the use of the following expression:

new_Pi = old_Pi + sin(old_Pi)

Step 3: Calculate the “error” of the new approximation by the expression:

error = new_Pi – old_Pi

Step 4: Store the new approximation into the old one by doing:

old_Pi = new_Pi

Step 5: Repeat the calculations from Step 2.

The process should be stopped when the error calculated in Step 3 falls below a chosen (very small) number called “epsilon” (hence, in your program you need to prompt the user for the initial guess and the selectedvalue of epsilon.) After the iterations are stopped, the best value of p generated by the iterations will be stored in “new_Pi” and must be sent to the output screen.

Hint 1: use the While Loop template to code this program.

Hint 2: write the following lines of code immediately after your variable declarations:             cout.setf(ios::fixed);

            cout.setf(ios::showpoint);

            cout.precision(10);   // This will display 10 significant digits in the output

Explanation / Answer

function numdigits(x: integer): integer;

var t,n: integer;

begin

n := 1; t := x;

while t >= 10 do begin

n := n + 1;

t := t div 10;

end;

numdigits := n;

end;

(c)

numdigits x = if x < 10 then 1 else numdigits(x / 10) + 1

(e)

function numdigits(x: Integer) return Integer is

t: Integer := x;

n: Integer := 1;

begin

while t >= 10 loop

n := n + 1;

t := t / 10;

end loop;

return n;

end numdigits;

(g)

class NumDigits

{ public static int numdigits(int x)

{ int t = x, n = 1;

while (t >= 10)

{ n++;

t = t / 10;

}

return n;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote