Question

In: Computer Science

Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You...

Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You write a sequence of numbers, one after another. Start by writing n. If n is even, then the next number is n/2. If n is odd, then the next number is 3n + 1. Continue in this way until you write the number 1.

For example, if you start at 7, then the next number is 22 (3 × 7 + 1). The next number after 22 is 11.

  • The hailstone sequence starting at 7 is [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] and its length is 17.

  • The hailstone sequence starting at 6 is [6, 3, 10, 5, 16, 8, 4, 2, 1] and its length is 9.

  • The hailstone sequence starting at 1 is [1] and its length is 1.

The Assignment

Write and test a C++ program that reads a number n from the standard input (after giving a suitable prompt) and then writes the following information on the standard output:

  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. an indication of whether the hailstone sequence that starts with n contains a number that is greater than 1000.

  5. the length of the longest hailstone sequence that starts with a number from 1 to n;

  6. the starting number of the longest hailstone sequence that starts with a number from 1 to n;

  7. the largest number that occurs in any hailstone sequence that starts with a number from 1 to n.

  8. the start value, from 1 to n, of the hailstone sequence that contains largest number reported in the previous step.

For this program, use loops. Do not use recursion. Use type int for all of the integers. Do not use any of

  • arrays.
  • call-by-reference,
  • any features of the C++ Standard Template Library,
  • default parameters,
  • global or static variables

The main function must not contain any loops. You can use the <cstdio>, <iostream> and <algorithm> libraries for this assignment.

The output needs to be sensible and easy to read, not just numbers. It must follow the general template below, with all numeric results lined up (approximately) vertically. Each part of the output should be on a separate line. Parts in black are written by the program.

  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
  Sequence length:                                      17
  Largest number:                                       52
  Contains a number >1000?                              no
  Greatest length starting with 1 to 7:                 17
  Start value of sequence of length 17:                  7
  Largest value in a sequence starting with 1 to 7:     52
  Start value of sequence containing 52:                 7

Solutions

Expert Solution

Code:

#include <iostream>

using namespace std;

int hailStone(){

    int i=1, n, max=0, max2=0, max_index, length=1, ln=0, initial;

    cout << "What number shall I start with?\n";

    cin >> n;

    cout << "The Hailstone sequence starting with " << n << "is:\n";

    initial=n;

    do{

        if(n>max){

            max=n;

            max_index=i;

        }

        if(max>1000)

            ln=1;

        if(n>max2 && i<n)

            max2=n;

        cout << n << " ";

        if(n%2==0)

            n/=2;

        else

            n=3*n+1;

        length++;

        i++;

    }while(n!=1);

    cout << "1" << endl;

    cout << "Sequence length: " << length << endl;

    cout << "Largest number: " << max << endl;

    cout << "Contains a no. greater than 1000?: ";

    if(ln==1)

        cout << "YES" << endl;

    else

        cout << "NO" << endl;

    cout << "Greatest length starting with 1 to " << length << ":" << max2 << endl;

    cout << "Starting value of length " << length << " : " << initial << endl;

    cout << " Largest value in a sequence starting with 1 to "<<initial << ":" << max << endl;

    cout << "Start value of sequence containing " << max << " :" << initial << endl;

    return 0;

}

int main(){

    hailStone();

    return 0;

}

Output:


Related Solutions

Prove that τ(n) < 2 n for any positive integer n. This is a question in...
Prove that τ(n) < 2 n for any positive integer n. This is a question in Number theory
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
Use induction to prove that for any positive integer n, 8^n - 3^n is a multiple...
Use induction to prove that for any positive integer n, 8^n - 3^n is a multiple of 5.
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Determine the number of permutations of {1,2,3,...,n-1,n} where n is any positive integer and no even...
Determine the number of permutations of {1,2,3,...,n-1,n} where n is any positive integer and no even integer is in its natural position.
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
HOW TO ANSWER IN PYTHON : PROBLEM: Given a positive integer (call it ​N), a position​...
HOW TO ANSWER IN PYTHON : PROBLEM: Given a positive integer (call it ​N), a position​ ​in that integer (call it ​P), and a transition integer (call it ​D). Transform ​N as follows: If the ​Pth​ digit of ​N from the right is from​ ​0 to 4, add ​D to it. Replace the ​Pth​ digit by the units digit of the sum. Then, replace all digits to the right of the ​Pth​ digit by 0. If the ​Pth​ digit of...
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7,...
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7, output: 2,3,5,7,11,13,17. In Java please
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Let n be a positive integer. Prove that if n is composite, then n has a...
Let n be a positive integer. Prove that if n is composite, then n has a prime factor less than or equal to sqrt(n) . (Hint: first show that n has a factor less than or equal to sqrt(n) )
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT