Good morning everyone,
The library will be home, 0800 till 2100. I've been working on trying my loops in my hailstone sequence into recursions. It seem recursion can easrier to do, instead of creating loops. I say seem, because now I have come to halt on my progress. The halt is in the form of my sequence_Max function:
/*
//sequence_Max(int n)
//Returns the largest value in the
//hailstone sequence starting at 'n'.
*/
int sequence_Max(int n)
{
//Variable Declaration.
int max = n, temp = n;
while (temp > 1)
{
temp = next(temp);
if(temp > max)
{
max = temp;
}
}
return max;
}
It was a pain in the neck coming up with a loop (I recieved a lot of help from individuals on this site) for it, but now I have to change the loop into a recursion. The only practice that I have up to this point with recursion is in the hailstone_Sequence and sequence_Length functions.
Below is my program thus far (I don't have to moditfy the the next or main functions):
// Tab stops: 4
//The program executes a number of functions to print their
//values with a list of sentences.
#include <iostream>
#include <cstdio>
using namespace std;
//Function Prototype Declaration
int next(int), sequence_Length(int), sequence_Max(int), sequence_Comparison(int),
initialcomp_Num(int);
void hailstone_Sequence(int);
int main (int argc, char** argv)
{
int user_Input;
cout << "What number shall I start with? ";
cin >> user_Input;
cout << user_Input << "\n";
cout << "The hailstone sequence starting at " << user_Input << " is: " << endl;
hailstone_Sequence(user_Input);
cout << "\nThe length of the sequence is " << sequence_Length(user_Input);
cout << ".\n";
}
/*
//next(int n)
//Returns a number
//that follows 'n' in the sequence.
*/
int next(int n)
{
if (n > 1)
{
if ((n % 2) == 0)
{
return n / 2;
}
else
{
return 3 * n + 1;
}
}
return n;
}
/*
//hailstone_Sequence(int n)
//Prints the entire sequence beginning with 'n'.
*/
void hailstone_Sequence(int n)
{
//Variable Declaration.
int temp = n;
//Base case.
if (n == 1)
{
cout << 1;
}
else if (n > 1)
{
cout << n << " ";
hailstone_Sequence(next(n));
}
}
/*
//sequence_Length(int n)
//Returns the output length of
//the hailstone sequence.
*/
int sequence_Length(int n)
{
int count = next(n);
//Special case statement.
if (n == 1)
{
return 1;
}
//Determines the length of the sequence until
//n is equal to 1 (base case).
else if (count >= 1)
{
count;
return 1 + sequence_Length(count);
}
}
/*
//sequence_Max(int n)
//Returns the largest value in the
//hailstone sequence starting at 'n'.
*/
int sequence_Max(int n)
{
//Variable Declaration.
int max = n, temp = n;
while (temp > 1)
{
temp = next(temp);
if(temp > max)
{
max = temp;
}
}
return max;
}
/*
//sequence_Comparison(int n)
//Returns the integer with the longest hailstone
//sequence starting from 1 to 'n'.
*/
int sequence_Comparison(int n)
{
//Variable Declaration.
int count, top, aftertop;
if ( n <= 1)
{
return n;
}
top = sequence_Length(n);
for (count = 1; count != n; count++)
{
if (top < sequence_Length(count))
{
aftertop = sequence_Length(count);
top = aftertop;
}
}
return top;
}
/*
//initialcomp_Num(int n)
//Returns the starting value
//of the longest sequence that starts on a value from
//1 to 'n'.
*/
int initialcomp_Num(int n)
{
//Variable Declaration.
int count, initial, topValue, cal;
//Function will work as intended if 'n' is not less than 1.
if (n <= 1)
{
return n;
}
initial = sequence_Length(n);
topValue = n;
for (count = 1; count != n; count++)
{
cal = sequence_Length(count);
//max(initial, cal)
if (initial <= cal)
{
//initial = cal;
topValue = count;
}
else if (initial >= cal)
{
topValue = topValue;
}
}
return topValue;
}
Limitations
a). No function is allowed to change the value of any variable, once that variable has been given a value.
To get started with the sequence_Max function, should I look into using a max(x,y) calculation?
v/r