Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

All the function needs to be recrusion. I need to Write a contract, then an implementation , of a function that takes an integer n

All the function needs to be recrusion. I need to Write a contract, then an implementation, of a function that takes an integer n and returns the length of the longest hailstone sequence starting at a number from 1 to n. This function must not write anything. I also need to Write a contract, then an implementation, of a function that takes an integer n and returns the start value k, from 1 to n, that has the longest length. Both of these function needs to be after largest.

Example:

What number shall I start with? 7

The hailstone sequence starting at 7 is: 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

The length of the sequence is 17.

The largest number in the sequence is 52.

The longest hailstone sequence starting with a number up to 7 has length 17

The longest hailstone sequence starting with a number up to 7 begins with 7

After that I needed to 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).

Suppose that you want to find the length of the hailstone sequence starting at 7. 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 1 or not.

#include #include using namespace std; //====================================================================== // next(n) returns the number that follows n in the hailstonesequence. // For example, next(7) = 22 and next(22) = 1 and next(1) = 1. //======================================================================

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

//====================================================================== // sequence(n) takes the integer n and outputs the integer in the // hailstorm sequence starting with the user input and ends when the // sequence equals 1. //======================================================================

void sequence(int n) { cout << n << " "; if(n!=1) { n=next(n); sequence(n); } }

//====================================================================== // lenght(n) takes the integer n and counts all the numbers that are in // the hailstorm sequence and adds them up. //======================================================================

int length(int n) { if ( n == 1) return 1; else return 1 + length(next(n)); }

//====================================================================== // large(n) takes the integer n and checks to see if the number is larger // than n and then returns the largest number in the hailstormsequence //======================================================================

int largest1(int n) //largest of the sequence that start with n { if(n==1) // if n ==1 , then it have only one element 1, return it return 1; // else, largest of the sequence = maximum(n, largest of the sequence that start with next(n))

int maxint = largest1(next(n)); // largest of the sequenc that start with next(n) and store it in maxint return maxint>n? maxint:n; // return maximum of n, maxint }

int main(int argc, char** argv) { int n; printf("What number shall I start with? "); scanf("%i", &n); printf("The hailstone sequence is starting with %i is ", n); sequence(n); printf(" "); printf("The lenght of the sequence is %i ", length(n)); printf("The largest number in the sequence is %i ", largest1(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 Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions