Question
Need help with 4-7 in my C++ program The entire hailstone sequence starting at n , all on one line, with the numbers separated by
Need help with 4-7 in my C++ program
-
The entire hailstone sequence starting at n, all on one line, with the numbers separated by spaces.
-
The length of the hailstone sequence that starts with n.
-
The largest number in the hailstone sequence that starts with n.
-
A longest hailstone sequence that starts with a number from 1 to n. (There might be two or more equally long sequences. Only one of those should be shown.)
-
The length of the hailstone sequence shown in part 4.
-
A hailstone sequence that starts with a number from 1 to n that contains the largest number.
-
The largest number in the hailstone sequence shown in part 6.
________________________________________________________________________
What number shall I start with? 16 sequence: 16 8 4 2 1 length: 5 largest: 16 For start values from 1 to 16: longest: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 length: 20 containing largest: 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 largest: 160
_________________________________________________________________________
What I have some far
_______________________________________________________
#include
// Creates the next input in a sequence int next(int n) { int x = n;
if (x % 2 == 0) { x = x / 2; return x; } else { x = 3 * x + 1; return x; } }
// Prints the whole sequence for your input void PrintSequence(int n) { for(int x = n; x > 1; x = next(x)) { printf("%i ", x); } printf("1"); }
int length(int n) { int count = 1; int x = n;
while(x > 1) { x = next(x); count++; } return count; } // Creates the largest number in the first sequence int largest(int n) { int max = n; int x = n;
while(x != 1) { if(max
int longestStart(int n) { }
int largestStart(int n) { }
int main() { int n = 0; printf("What number shall I start with? "); scanf("%i",&n); printf("sequence: "); PrintSequence(n); printf(" length: %i",length(n)); printf(" largest: %i",largest(n)); printf(" For start values from 1 to %i: ",n); printf(" longest: "); printf("length: "); printf(" containing largest: "); printf("largest: ");
return 0; }
_________________________________________________________
16 What number shall I start with? sequence: 16 8 4 2 1 length: 5 largest: 16 For start values from 1 to 16: longest: length: containing largest: largestStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started