In: Computer Science
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:
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;
an indication of whether the hailstone sequence that starts with n contains a number that is greater than 1000.
the length of the longest hailstone sequence that starts with a number from 1 to n;
the starting number of the longest hailstone sequence that starts with a number from 1 to n;
the largest number that occurs in any hailstone sequence that starts with a number from 1 to n.
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
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
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: