# Cs questions | Computer Science homework help

1. Write a recursive algorithm to compute a+b, where a and b are nonnegative integers.

2. Let a be an array of integers. Write a recursive algorithm to compute the average of the elements of the array.

Solutions in which the sum is recursive (instead of the average) are worth fewer points. Note stopping case.

3. Ackerman’s function is defined recursively on the nonnegative integers as follows:

a(m, n) = n + 1 if m == 0

a(m, n) = a(m-1, 1) if m != 0, n == 0

a(m, n) = a(m-1, a(m, n-1)) if m != 0, n != 0

Using the above definition, show that a(2,2) equals 7.

4. If an array contains n elements, what is the maximum number of recursive calls made by the binary search algorithm?

5. The expression m % n yields the remainder of m upon division by n. Define the greatest common divisor (GCD) of two integers x and y by:

gcd(x,y) = y if ( y <= x && x % y == 0) if ( y <= x && x % y == 0)

gcd(x,y) = gcd(y, x) if (x < y )

gcd(x, y) = gcd(y, x % y) otherwise

Write a recursive method to compute gcd(x,y).

6. Define a generalized fibonacci sequence of f0 and f1 as the sequence gfib( f0, f1, 0), gfib(f0, f1, 1), gfib(f0, f1, 2), …, where

gfib(f0, f1, 0) = f0

gfib(f0, f1, 1) = f1

gfib(f0, f1, n) = gfib(f0, f1, n-1) + gfib(f0, f1, n-2) if n> 1

Write a recursive method to compute gfib(f0,f1,n).

7. Show how to transform the following iterative procedure into a recursive procedure. f(i) is a method returning a logical value based on the value of i, and g(i) is a method that returns a value with the same attributes as i. 605.202 Homework 3

void iter(int n)

{

int i;

i = n;

while ( f(i) == TRUE ) {

/* any group of statement that */

/* does not change the value of i */

i = g(i);

} // end while

} //end iter

8. Convert the following recursive program scheme into an iterative version that does not use a stack. f(n) is a method that returns TRUE or FALSE based on the value of n, and g(n) is a method that returns a value of the same type as n (without modifying n).

int rec(int n)

{

if ( f(n) == FALSE ) {

/* any group of statement that */

/* do not change the value of n */

return (rec(g(n)));

}//end if

}//end rec

Pages (275 words)
Standard price: \$0.00
Client Reviews
4.9
Sitejabber
4.6
Trustpilot
4.8
Our Guarantees
100% Confidentiality
Information about customers is confidential and never disclosed to third parties.
Original Writing
We complete all papers from scratch. You can get a plagiarism report.
Timely Delivery
No missed deadlines – 97% of assignments are completed in time.
Money Back