Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix this C++ below so does not use any loops at all . Use recursion instead. here is some guides given. Create a directory to

Fix this C++ below so does not use any loops at all. Use recursion instead. here is some guides given.

Create a directory to hold assignment 3. Copy files hailstone.cpp, Makefile and dotestassignment 2 to the assignment 3 directory. Edit the comments at the top of hailstone.cppto say that this is assignment 3.

You should be able to keep the same contracts, 'next' function and 'main' function from assignment 2, unless they contained errors that needed to be fixed. If necessary, edit those functions so that they do not change the value of any variable, once the variable has a value.

2. Modify the other functions so that they use recursion instead of loops. Modify them one at at time, and test each one after modifying it. Do not try to modify them all then test them all together. The remaining items give some hints.

3. Modify the function that prints the entire sequence. Include a special case for n = 1. When n > 1, the sequence that starts at n contains n followed by the sequence that starts at next(n).

4. Suppose that you want to find the length of the hailstone sequence starting at 7. Notice that next(7) = 22. If you ask for the length of the hailstone sequence starting at 22 (result: 16), how would you determine the length of the sequence starting at 7?

Handle the length of the sequence starting at 1 as a special case. Use an if-statement to decide whether this is the special case of n = 1 or not.

5. Suppose that you want to know the largest number in the sequence starting at 7. Ask for the largest number in the sequence starting at 22. (The answer is 52.) The largest number for 7 is clearly the larger of 7 and 52, since 7 is the only number that was not already taken into account in the sequence starting at 22.

As another example, suppose that you want to know the largest number in the sequence starting at 52. Ask for the largest number in the sequence starting at 26 (since next(52) = 26). The answer is 40. What you want is the larger of 52 and 40: clearly 52.

Important Note. In terms of efficiency, recursion acts as an amplifier. Algorithm designers use it because, if they think of a clever idea and they use it in a recursive definition, recursion amplifies it into a very efficient algorithm.

But recursion also amplifies bad ideas. If you do something in a slopply way in a recursive function, recursion can amplify it and make a very slow algorithm. In particular, if you do the same recursive call twice, which is a waste of time, recursion will be very slow. Try your program on input 97. Does it respond quickly, or is it really slow? Try it on a few larger numbers, say around 1000.

6. Suppose you want to compute the length of the longest sequence starting on a number from 1 to n. Ifn = 1, the answer is obvious. If n > 1, first get the length k of the longest sequence starting on a number from 1 to n1. Then compute the larger of length(n) and k.

7. Suppose you want to compute the starting value of the longest sequence starting on a number from 1 to n. Again, the answer is obvious when n = 1. If n > 1, first get the starting value m of the longest sequence starting on a number from 1 to n1. Then compare length(n) and length(m). How can you determine the answer from the result of that comparison?

HERE IS THE CODE Fix this C++ below so does not use any loops at all. Use recursion instead.

#include #include #include using namespace std; // Next(n) returns the number that follows n in a hailstone sequence. // For example, next(7) = 22 and next(8) = 4. // // Next requires n > 1, since there is no number that follows 1.

int next(int n) { int x = n;

if (x % 2 == 0) { return x / 2; } else { return 3 * x + 1; } }

//writeHailstoneSequence takes (int n) and writes the, //entire hailstone sequence starting at n..

void writeHailstoneSequence(int n) { for(int x = n; x > 1; x = next(x)) { printf("%i ", x); } printf("1 "); }

//lengthHailstone(int n) returns the length of the //hailstone sequence starting at n.

int lengthHailstone(int n) { int count = 1; int x = n;

while(x > 1) { x = next(x); count++; } return count; }

//hailLargest(n) returns the largest value in, // the hailstone sequence starting at n it does not print, //anything to the screen. int hailLargest(int n) { int ans = n; int x = n;

while(x != 1) { if(ans <= x) { ans = x; } x = next(x); } return ans; }

//hailLargestLength(n) returns the largest //possible length of a sequence that starts //from 1 to n.

int hailLargestLength(int n) { int x = n; int ans = lengthHailstone(x);

if(x == 1) { return 1; }

while(x > 1) { if(ans <= lengthHailstone(x)) { ans = lengthHailstone(x); } x = x - 1; } return ans; }

//hailLargestStartValue(n) returns the start //value of the longest hailstone sequence //starting with a number from 1 to n. // It does not read or write anything.

int hailLargestStartValue(int n)

{

int x = n;

int val = n;

int ans = lengthHailstone(x);

if (x == 1)

{

return 1;

}

while (x > 1)

{

if (ans <= lengthHailstone(x))

{

ans = lengthHailstone(x);

val = x;

}

x = x - 1;

}

return val;

}

//sequenceLargestValue(n) returns the //largest value that occurs in a hailstone //sequence that starts with a number from 1 to n. // if the largest value is 1 it just returns 1. int sequenceLargestValue(int n)

{

int x = n; int ans = hailLargest(x); if (x == 1)

{

return 1;

}

while (x > 1)

{

if (ans <= hailLargest(x))

{

ans = hailLargest(x);

}

x = x - 1;

}

return ans;

}

int main() { int n = 0; printf("What number shall I start with? "); scanf("%i", &n);

printf("The hailstone sequence starting at %i is: ", n); writeHailstoneSequence(n);

printf(" The length of the sequence is %i. ", lengthHailstone(n));

printf("The largest number of the sequence is %i. ", hailLargest(n));

printf("The longest hailstone sequence starting with a number up to %i has length %i. " , n, hailLargestLength(n));

printf("The longest hailstone sequence starting with a number up to %i begins with %i. " , n, hailLargestStartValue(n));

printf("The largest value in any hailstone sequence starting with a number up to %i is %i. " ,n, sequenceLargestValue(n)); return 0; }

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

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

=+ (a) Show that the definition is consistent.

Answered: 1 week ago