Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The method below attempts to calculate xn economically: public double pow(double x, int n) { double y; if (n == 1) y = x; else

The method below attempts to calculate xn economically:

public double pow(double x, int n)

{

double y;

if (n == 1)

y = x;

else

y = pow(x, n/2) * pow(x, n - n/2); // Line 7

return y;

}

(a) How many multiplications will be executed when pow(1.234, 5) is called?

(b) How many multiplications will be executed if we replace Line 7 above with the following statements?

{ y = pow(x, n/2); y *= y; if (n % 2 != 0) y *= x; }

(c) How many multiplications will Version (a) above take to calculate pow(1.234, 9)? Version (b)?

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

Students also viewed these Databases questions