// Tab stops: 4
//The program will use a given integer from the user to compute a
//a particular sequence. Tab tops 4.
#include <iostream>
#include <cstdio>
usingnamespace std;
//Function Variable Declaration.
int hailstone_Sequence(int n), lengthof_Sequence(int x);
//int[] hailstone_List [x];
//void Print_Set(int [] set, int size);
int main(int argc, char** argv)
{
int user_Input; cin >> user_Input;
cout << "What number shall I start with? " << user_Input << endl;
cout << "The hailstone of sequence starting at " << user_Input << " is: " << endl;
cout << Print_Set(int [] set, int ))
cout << "The length of the sequence is ___. \n";
cout << "The largest number in the sequence is: \n";
cout << "The longest hailstone sequence with a number up to __ has length ___ ";
cout << "The longest hailstone sequence with a number up to __ begins with ___ ";
return 0;
}
/* The hailstone sequence takes an integer (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 integer 1
// is reached.
*/
int hailstone_Sequence (int n)
{
int x = 0;
if (n > 1)
{
while (n != 1)
{
if ((n % 2) == 0 )
{
n = n / 2;
}
else
{
n = (3*n) + 1;
}
x++;//Will be used for the array's lenght
//Array function here;
}
return n;
}
}
// int lengthof_Sequence(int x);
// {
//
//
//
//
//
// }
// void Print_Set(int [] set, int size)//Still working on function
// {
for(int index = 0; index < size; index++) {
System.out.print(set[index]);
//Print commas after every integer except the last one
if(index < size - 1){
System.out.print(", ");
}
}
//
//
//
// }
The program is to use an integer provided by the user for a Hailstone Sequence, then to list the Sequence out. Example:
user integer: 22
list: #, #, #, #, #,
I have an idea on what I would like done, but not sure how to go about it in cpp. Only have little under a month in this language. I'm looking to take the values from the hailstone_Sequence to be in array for display. Any advice would be much appreciated. Looking to learn as much as I can.
If I'm not looking to place the value of the sequence in an array, then I have no use for 'x'. I came up with 'x' for it to later be potentially used as the array maxsize. How would I be able to print out each value from the sequence on one line without doing an array?
#include <iostream>
// assumes that if n is odd 3*n+1 would be within the range of unsigned int
unsignedint next( unsignedint n ) { return n%2 == 0 ? n/2 : 3*n + 1 ; }
void print_sequence( unsignedint n )
{
if( n == 0 ) return ;
unsignedint largest = 1 ;
unsignedint length = 0 ;
while( n != 1 )
{
std::cout << n << ", " ;
++length ;
if( n > largest ) largest = n ;
n = next(n) ;
}
std::cout << "1\n"
<< "length: " << length+1 << '\n'
<< "largest value: " << largest << '\n' ;
}