Question: My assignment is to write a program in C++ that runs a hailstone sequence. I have the jist of the assignment down, but I am

My assignment is to write a program in C++ that runs a hailstone sequence. I have the jist of the assignment down, but I am not allowed to use recursion in my loops and I cannot figure out how to rewrite them.

When the program runs correctly it should look like:

 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.

Here is another example.

 What number shall I start with? 1 The hailstone sequence starting at 1 is: 1 The length of the sequence is 1. The largest number in the sequence is 1. The longest hailstone sequence starting with a number up to 1 has length 1. The longest hailstone sequence starting with a number up to 1 begins with 1.

The program that I currently have is.

#include #include using namespace std;

// next(int n) returns the number that follows n in the hailstone sequence.

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

// sequence(int n) takes the integer n and outputs the integer in the // hailstone sequence starting at (n).

void sequence(int n) { if (n==1) {

printf("%d ", n); } else { printf("%d ", n); sequence(next(n)); } }

// length(int n) takes the integer n and counts all the numbers that are in // the hailstone sequence and adds them up.

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

// largest(int n) returns the largest number in the // hailstone sequence starting at n.

int largest(int n) { if(n==1) { return 1; } else { return max(largest(next(n)), n); } }

// longestHailstone(int n) returns the longest of the hailstone sequence // that starts with numbers from 1 to n.

int longestHailstone(int n) { if(n==1) { return 1; } else { return max(length(n),longestHailstone(n-1)); } }

// firstLongest(n) returns the first number of the longest hailstone // sequence that starts with numbers from 1 to n.

int firstLongest(int n) { if(n==1) { return 1; } else { int a = max(length(n),longestHailstone(n-1)); int count = 1; int i = 1; while(i < n) { if(a > length(i)) { count = count+1; } else if(a = length(i)) { return count; } i++; } return count; } }

int main(int argc, char** argv) { int n; printf("What number shall I start with? "); scanf("%d", &n); printf("The hailstone sequence starting with %d is ", n); sequence(n); printf(" "); printf("The length of the sequence is %d ", length(n)); printf("The largest number in the sequence is %d ", largest(n)); printf("The longest hailstone sequence starting with a number up to %d has length %d ",n, longestHailstone(n)); printf("The longest hailstone sequence starting with a number up to %d begins with %d ", n, firstLongest(n)); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!