Question

Please follow the insturctions and solve it by C++ ASAP. The hailstone sequence is a series...

Please follow the insturctions and solve it by C++ ASAP.

The hailstone sequence is a series of numbers that can be generated with an algorithm that takes an input number n. The sequence is defined as:

If n is 1, the sequence stops.

If n is even, the next number is n / 2.

If n is odd, the next number is 3n + 1.

For example, if we start with n = 10, the sequence is:

10, 5, 16, 8, 4, 2, 1

This sequence is interesting because it appears that given any number, the sequence will always reach 1 (and stop). Unfortunately, some numbers take many steps to reach 1.

Write a recursive function that prints out each step of the hailstone sequence given n, but also accepts an parameter to limit the number of steps to some value. Only the function is needed (don't write an accompanying main()).

Homework Answers

Answer #1

ANSWER:-

#include <iostream>
using namespace std;
void HailstoneNumbers(int N,int s)
{
cout << N << " ";
if (N == 1 || s==1) {
return;
}
else if (N % 2 == 0) {
// If N is Even.
HailstoneNumbers(N / 2,s-1);
}
else if (N % 2 != 0) {
// N is Odd.
HailstoneNumbers(3 * N + 1,s-1);
}
}
// Driver function
int main()
{
int N,s;
cout<<"Enter a number : ";
cin>>N;
cout<<"Enter the maximum number of steps : ";
cin>>s;
cout<<"the sequence is: ";
HailstoneNumbers(N,s);
return 0;
}

// OUTPUT

// If any doubt please comment

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...
1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function...
1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function sequence that takes one parameter: n, which is the initial value of a sequence of integers. The 3n + 1 sequence starts with an integer, n in this case, wherein each successive integer of the sequence is calculated based on these rules: 1. If the current value of n is odd, then the next number in the sequence is three times the current number...
Provide a recursive definition of some sequence of numbers or function (e.g. log, exponent, polynomial). Choose...
Provide a recursive definition of some sequence of numbers or function (e.g. log, exponent, polynomial). Choose one different from that of any posted thus far. Write a recursive method that given n, computes the nth term of that sequence. Also provide an equivalent iterative implementation. How do the two implementations compare?
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...
We are given a sequence of numbers: 1, 3, 5, 7, 9, . . . and...
We are given a sequence of numbers: 1, 3, 5, 7, 9, . . . and want to prove that the closed formula for the sequence is an = 2n – 1.          What would the next number in the sequence be? What is the recursive formula for the sequence? Is the closed formula true for a1? What about a2? What about a3? Critical Thinking How many values would we have to check before we could be sure that the...
Write a program that solves exercises 1.3 or 1.5. If you solve both correctly, 20 extra...
Write a program that solves exercises 1.3 or 1.5. If you solve both correctly, 20 extra points will be given. For Exercise 1.3, you can use only the following C++ function or its Java equivalent to output the results of your program: void printDigit(char c ){     if (c=='-' || c=='+' || c=='.' || c == '\n' || (c>='0' && c<='9')){         cout << c;         return;     }          cout << "Invalid characters." << endl;     return;     ...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...
language: JAVA the Problem Below are a series of problems you need to solve using recursive...
language: JAVA the Problem Below are a series of problems you need to solve using recursive methods. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck   Write a recursive method that checks whether an array of integers -...
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT