Question

In: Computer Science

Consider the iterative method below designed to sum a range of values from x to y....

Consider the iterative method below designed to sum a range of values from x to y.

public static int fun(int x, int y) {

int ans = 0;

for(int i = x; i <= y; i++)

ans = ans + i;

return ans;

}

Which of the following recursive methods are algorithmically equivalent to the iterative method shown above:

I.

public static int fun(int x, int y) {

if(x == y)

return 0;

else return x + (x + 1, y);

}

II.

public static int fun(int x, int y) {

if(x == y)

return 0;

else return y + (x, y - 1);

}

III.

public static int fun(int x, int y) {

if(x == y)

return 0;

else return y + (x + 1, y);

}

A. I only

B. I and II only

C. I and III only

D. II only

Solutions

Expert Solution

Itrative method

public static int fun(int x, int y)

{

int ans = 0;

for(int i = x; i <= y; i++)

ans = ans + i;

return ans;

}

assume x = 1 and y = 4

so here for loop iterate 1 to 4 then

ans = 1 + 2 + 3 + 4

ans = 10

Recursive method

1) public static int fun(int x, int y)

{

if(x == y)

return 0;

else return x + (x + 1, y);

}

same assume here x = 1 and y = 3

call fun(1 , 4)

1 + fun(2 , 4 )

2 + fun (3 , 4)

3 + fun(4 , 4)

fun (4 , 4) true return 0  

fun (4 , 4) = 0

fun(3 , 4) = 3 + 0

fun (2, 4) = 2 + 3 + 0

fun(1 , 4) = 1 + 2 + 3 + 0

return 6

it 's not equivalent to itaretive method

---------------------------------------------------------------------------------------------

2) public static int fun(int x, int y)

{

if(x == y)

return 0;

else return y + (x, y - 1);

}

same assume here x = 1 and y = 4

call fun(1 , 4)

4 + fun(1 , 3 )

3 + fun (1 , 2)

2 + fun(1 , 1)

fun (1 , 1) true return 0  

fun (1 , 1) = 0

fun(1 , 2) = 2 + 0

fun(1, 3) = 3 + 2 + 0

fun(1 , 4) = 4 + 3 + 2 + 0

return 9

it 's not equivalent to itaretive method

---------------------------------------------------------------------------------------------

3) public static int fun(int x, int y)

{

if(x == y)

return 0;

else return y + (x + 1, y);

}

same assume here x = 1 and y = 4

call fun(1 , 4)

4 + fun(2 , 4 )

4 + fun (3 , 4)

4 + fun(4 , 4)

fun (4 , 4) true return 0  

fun (4 , 4) = 0

fun(3 , 4) = 4 + 0

fun(2 , 4) = 4 + 4 + 0

fun(1 , 4) = 4 + 4 + 4 + 0

return 12

it 's not equivalent to itaretive method

---------------------------------------------------------------------------------------------

all three recursive method are not equivalent to the iterative method,


Related Solutions

Calculate the Y values corresponding to the X values given below.  Find the critical values for X...
Calculate the Y values corresponding to the X values given below.  Find the critical values for X for the given polynomial by finding the X values among those given where the first derivative, dy/dx = 0 and/or X values where the second derivative, d­2y/dx2 = 0.    Be sure to find the sign (+ or -) of  dy/dx and of d2y/dx2 at all X values. Reference Lesson 13 and the text Appendix A (pp 694 – 698), as needed.  Using the first and second derivative...
Below are the values for two variables x and y obtained from a sample of size...
Below are the values for two variables x and y obtained from a sample of size 5. We want to build a regression equation based the sample data. ŷ = b₀ + b₁x y x 16 5 21 10 8 6 28 12 53 14 11 On average the observed y deviate from the predicted y by, a 10.73 b 10.04 c 9.53 d 8.76 12 Sum of squares total (SST) is, a 1096.3 b 1178.8 c 1296.7 d 1361.5...
Below are the values for two variables x and y obtained from a sample of size...
Below are the values for two variables x and y obtained from a sample of size 5. We want to build a regression equation based the sample data. ŷ = b₀ + b₁x y x 16 5 21 10 8 6 28 12 53 14 1 The sum product of x and y is, a 1416 b 1451 c 1466 d 1481 2 The value in the numerator of the formula to compute the slope of the regression equation is,...
What is the function and argument to calculate the sum of the values in the range...
What is the function and argument to calculate the sum of the values in the range D1:D50 for which in the adjacent value in the range A1:A50 equals “Senior” and the adjacent value in the range B1:B50 equals “B”? What is the function and argument to calculate the average of the cells in the range C1:C50 for which the adjacent cell in the range B1:B50 equals “B”? What is the function and argument to return the index number of the...
Calculate the Y values corresponding to the X values given below. Find the critical values for...
Calculate the Y values corresponding to the X values given below. Find the critical values for X for the given polynomial by finding the X values among those given where the first derivative, dy/dx = 0 and/or X values where the second derivative, d¬2y/dx2 = 0. Be sure to indicate the sign (+ or -) of dy/dx and of d2y/dx2 tabled values. Using the first and second derivative tests with the information you have calculated, determine which X value(s) represent...
Calculate the Y values corresponding to the X values given below. Find the critical values for...
Calculate the Y values corresponding to the X values given below. Find the critical values for X for the given polynomial by finding the X values among those given where the first derivative, dy/dx = 0 and/or X values where the second derivative, d­2y/dx2 = 0.    Be sure to find the sign (+ or -) of dy/dx and of d2y/dx2 at all X values. Reference Lesson 13 and the text Appendix A (pp 694 – 698), as needed. Using the...
Calculate the Y values corresponding to the X values given below. Find the critical values for...
Calculate the Y values corresponding to the X values given below. Find the critical values for X for the given polynomial by finding the X values among those given where the first derivative, dy/dx = 0 and/or X values where the second derivative, d­2y/dx2 = 0. Be sure to indicate the sign (+ or -) of dy/dx and of d2y/dx2 tabled values. Reference Power Point Lesson 13 as needed. Using the first and second derivative tests with the information you...
Calculate the Y values corresponding to the X values given below. Find the critical values for...
Calculate the Y values corresponding to the X values given below. Find the critical values for X for the given polynomial by finding the X values among those given where the first derivative, dy/dx = 0 and/or X values where the second derivative, d­2y/dx2 = 0. Be sure to indicate the sign (+ or -) of dy/dx and of d2y/dx2 tabled values. Reference Power Point Lesson 13 as needed. Using the first and second derivative tests with the information you...
Find the optimal values of x and y using the graphical solution method: Max x +...
Find the optimal values of x and y using the graphical solution method: Max x + 5y subject to: x + y ≤ 5 2x + y ≤ 8 x + 2y ≤ 8 x ≥ 0, y ≥ 0
Linear regression question If the range of the x-values are from x=100 to x=150, why might...
Linear regression question If the range of the x-values are from x=100 to x=150, why might we be reluctant to use our data to perform a hypothesis test on β0?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT