I'm currently working towards to provide the following output for my code:
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 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
#include <iostream>
#include <cstdio>
#include <vector>
usingnamespace std;
//Function Variable Declaration.
int lengthof_Sequence(int x), max_Sequence (int m), hailstone_Sequence (int n), count, length;
int main(int argc, char** argv)
{
int user_Input, max_Input;
cout << "What number shall I start with? "; cin >> user_Input; cout << user_Input << endl;
cout << "\nThe length of the sequence is " << hailstone_Sequence (user_Input) << ".";
cout << "\nThe largest number in the sequence is: \n";
cout << "The longest hailstone sequence starting " << user_Input << " with a number up to has length " << count<< "." <<endl;
cout << "The longest hailstone sequence starting with a number up to " << user_Input << " begins with " << user_Input;
return 0;
}
/* The hailstone sequence takes 'n' that is greater than 1 from the user. If
// 'n' is even, computes n/2. If n is odd, computes 3n+1. Both are done till n = 1
// is reached.
*/
int hailstone_Sequence (int n)
{
int count = 1, length = 0;
if (n > 1)
{
cout << "The hailstone of sequence starting at " << n << " is: " << endl;
cout << n << " ";
while (n > 1)
{
if ((n % 2) == 0 )
{
n = n / 2;
}
else
{
n = 3 * n + 1;
}
cout << n << " ";
count++;
}
length = count;
}
return count;
}
/* Lengthof_Sequence
// returns *pending*
//
*/
int lengthof_Sequence(int x)
{
}
int max_Sequence (int m)
{
}
I'm having trouble coming up with a way to print the max and this line "The longest hailstone sequence starting 7 with a number up to has length____." for the output. Does anyone have any advice or suggestions?
Well the way you have the functions currently requires you to generate the hailstone sequence inside of each function. And if you do generate the sequence in each one, you could also keep up with the max or add a counter variable.
#include <iostream>
#include <cstdlib>
#include <limits>
#include <typeinfo>
usingnamespace std;
//typedef unsigned long long INT;
//typedef unsigned long INT;
//typedef long INT;
typedefint INT;
INT MAX = numeric_limits<INT>::max();
INT TRIGGER = MAX / 3.0 + 0.9;
INT hailstoneSequence( INT nstart, bool showSequence, bool showLength );
//===========
int main( int argc, char *argv[] )
{
INT MaxLength = 0, nMaxLength = 0, length;
INT nmax;
INT i;
bool showLength = false;
bool showSequence = false;
if ( argc > 1 )
{ // maximum n taken from the command line
nmax = atoi( argv[1] );
}
else // maximum n taken from the user at run time
{
cout << "Enter the maximum n: ";
cin >> nmax;
cout << endl;
}
for ( i = 1; i <= nmax; i++ )
{
length = hailstoneSequence( i, showSequence, showLength );
if ( length > MaxLength )
{
MaxLength = length;
nMaxLength = i;
}
}
cout << endl;
cout << "The longest sequence is for n = " << nMaxLength << endl;
cout << "This sequence has length " << MaxLength << endl;
cout << "The sequence is "; hailstoneSequence( nMaxLength, true, false );
}
//==============
INT hailstoneSequence( INT nstart, bool showSequence, bool showLength )
{
INT n = nstart;
INT count = 1;
if ( showSequence || showLength ) cout << n;
while ( n != 1)
{
count++;
if ( n % 2 == 0 )
{
n /= 2;
}
elseif ( n >= TRIGGER )
{
cout << typeid(INT).name() << " bounds will be exceeded for starting point n = " << nstart << endl;
exit( 1 );
}
else
{
n = 3 * n + 1;
}
if ( showSequence ) cout << " " << n;
}
if ( showLength ) cout << " Length: " << count;
if ( showSequence || showLength ) cout << endl;
return count;
}
Using int type:
hailstone 10
The longest sequence is for n = 9
This sequence has length 20
The sequence is 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1