Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Tentamen (uitwerkingen)

Assignment 3 – Recursion Solutions | Johns Hopkins University CS 605.202

Beoordeling
-
Verkocht
-
Pagina's
4
Cijfer
A+
Geüpload op
29-01-2023
Geschreven in
2022/2023

Assignment 3 – Recursion Write pseudo-code not Java for problems requiring code. You are responsible for the appropriate level of detail. Q1 and Q2 are intended to help you get comfortable with recursion by thinking about something familiar in a recursive manner. Q3 – Q6 are practice in working with nontrivial recursive functions. Q7 and Q8 deal with the idea of conversion between iteration and recursion. 1. Write a recursive algorithm to compute a+b, where a and b are nonnegative integers. public int add(int a, int b) { if (b == 0) return a; else return 1 + add(a, b - 1); } 2. Let A be an array of integers. Write a recursive algorithm to compute the average of the elements of the array. Solutions calculating the sum recursively, instead of the average, are worth fewer points. public double average(int y[], int i) { double result; result = (double)y[i] / (double)h; if (i == 0) return result; else return result + average(y, i-1); } 3. If an array contains n elements, what is the maximum number of recursive calls made by the binary search algorithm? The binary search algorithm recursively searches half of the array. n = 1: 0 recursive calls n = 2: 1 recursive call n = 4: 2 recursive calls n = 8: 3 recursive calls n = 16: 4 recursive calls ... This pattern shows that the maximum number of recursive calls is generally ceiling(lg (n)). However, for the beginning cases of n = 1 and n = 2, an additional step could be required if the value is not in the list. Therefore, ceiling(lg(n)) + 1 is also acceptable. 4. The expression m % n yields the remainder of m upon (integer) division by n. Define the greatest common divisor (GCD) of two integers x and y by: gcd(x, y) = y if ( y ≤ x and x%y == 0) gcd(x, y) = gcd(y, x) if (x y )

Meer zien Lees minder
Instelling
Vak

Voorbeeld van de inhoud

Assignment 3 – Recursion

Write pseudo-code not Java for problems requiring code. You are responsible for the
appropriate level of detail.

Q1 and Q2 are intended to help you get comfortable with recursion by thinking about
something familiar in a recursive manner. Q3 – Q6 are practice in working with non-
trivial recursive functions. Q7 and Q8 deal with the idea of conversion between iteration
and recursion.

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

public int add(int a, int b) {
if (b == 0)
return a;
else
return 1 + add(a, b - 1);
}

2. Let A be an array of integers. Write a recursive algorithm to compute the average of the
elements of the array. Solutions calculating the sum recursively, instead of the average, are worth
fewer points.

public double average(int y[], int i) {
double result;
result = (double)y[i] / (double)y.length;
if (i == 0)
return result;
else
return result + average(y, i-1);
}

3. If an array contains n elements, what is the maximum number of recursive calls made by the
binary search algorithm?
The binary search algorithm recursively searches half of the array.
n = 1: 0 recursive calls
n = 2: 1 recursive call
n = 4: 2 recursive calls
n = 8: 3 recursive calls
n = 16: 4 recursive calls
...
This pattern shows that the maximum number of recursive calls is generally ceiling(lg (n)).
However, for the beginning cases of n = 1 and n = 2, an additional step could be required if
the value is not in the list. Therefore, ceiling(lg(n)) + 1 is also acceptable.

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

gcd(x, y) = y if ( y ≤ x and x%y == 0)
gcd(x, y) = gcd(y, x) if (x < y )

Geschreven voor

Vak

Documentinformatie

Geüpload op
29 januari 2023
Aantal pagina's
4
Geschreven in
2022/2023
Type
Tentamen (uitwerkingen)
Bevat
Vragen en antwoorden

Onderwerpen

$8.49
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper

Seller avatar
De reputatie van een verkoper is gebaseerd op het aantal documenten dat iemand tegen betaling verkocht heeft en de beoordelingen die voor die items ontvangen zijn. Er zijn drie niveau’s te onderscheiden: brons, zilver en goud. Hoe beter de reputatie, hoe meer de kwaliteit van zijn of haar werk te vertrouwen is.
ExamsConnoisseur Self
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
587
Lid sinds
3 jaar
Aantal volgers
344
Documenten
1492
Laatst verkocht
1 week geleden

4.2

68 beoordelingen

5
40
4
11
3
13
2
1
1
3

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen