Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  1. The entire hailstone sequence starting at n, all on one line, with the numbers separated by spaces.

  2. The length of the hailstone sequence that starts with n.

  3. The largest number in the hailstone sequence that starts with n.

  4. 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.)

  5. The length of the hailstone sequence shown in part 4.

  6. A hailstone sequence that starts with a number from 1 to n that contains the largest number.

  7. 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 using namespace std;

// 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; }

_________________________________________________________

image text in transcribed

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: largest

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

Oracle Database Upgrade Migration And Transformation Tips And Techniques

Authors: Edward Whalen ,Jim Czuprynski

1st Edition

0071846050, 978-0071846059

More Books

Students also viewed these Databases questions