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

Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more