Be seen. Boost your question’s priority for more expert views and faster solutions

Assignment: Write a program to read in a real number x and output the integer n closest to the cube root of x. Assume that x is always non-negative. The program should allow the user to repeat this calculation as often as desired.

We just covered functions, loops (not yet at arrays). Please provide the code. Thanks!

We just covered functions, loops (not yet at arrays). Please provide the code. Thanks!

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chadd, I could give you the code but it goes against my principle of completing another persons assignment. This way you won't learn anything. If you wish, I could give you the pseudo-code based on the Newton-Raphson method. You could then try to implement it & if you face any problem you can ask me more specific questions.

If you are still interested only in the code, I hope someone else can help you

Wilfred

Opps!! I guess I don't know how to read. hehe =] Darn it. Oh well, it will teach chaddd to get others to do his work.

To wpinto: I respect that. Any algorithm that you could provide would be helpful I'm sure. The 200 points is still at large.

If a^n = e^(n log a) where the base is e,

then the following function should work.

Function CubeRoot(x : Real) : Real;

Begin

If x = 0 Then

CubeRoot := 0

Else

CubeRoot := Exp(Ln(x) / 3);

End;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialThe above code will give you the cube root if you have access to functions such as log & exp. Also note that cube root of x is equivalent to x^(1/3), so if you have access to a power function its even easier.

The below algoritm needs only access to the addition & division operators

Here's the algorithm I promised

To compute y = x^(1/3)

y^3 = x

y^3-x = 0

Let f(y) = y^3-x

f'(y) = 3y^2

By Newton iteration:

y[n+1] = y[n] - f(y[n])/f'(y[n]) -------- f'(y[n]) <> 0

y[n+1] = y[n] - (y[n]^3-x)/(3y[n]^2) ---- 3y[n]^2 <> 0

y[n+1] = y[n] - (y[n]-x/y[n]^2)/3 ------- y[n] <> 0

y[n+1] = (2y[n] + x/y[n]^2) / 3 --------- x <> 0

Now the answer is in y[n] if y[n+1] = y[n]. Since we are dealing with floating point nos, its highly unlikely that you will reach the above condition. So you will have to check for a small difference between them eg. y[n+1] - y[n] < 0.000001. Note, the smaller the constant, the more precise the answer but the number of iterations will be more!

Here's the pseudo code

Function CubeRoot(x : Real) : Real;

VAR Yn : Real;

VAR Yn1 : Real;

Begin

If x = 0 Then

CubeRoot := 0

Else

Loop

{

Yn1 = ((Yn + Yn) + x / Yn / Yn) / 3;

if (absolute(Yn1 - Yn) < 0.00001)

exit loop;

Yn = Yn1;

}

CubeRoot := Yn1;

End;

Hope this helps

Wilfred

Pascal

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

begin

x=x*x*x;

cube=round(x);

end

Hope this helps.