I'm looking for help in a section of my C++ program. I need to "write a function that takes an integer n and returns the start value k, from 1 to n, that has the longest length."
I have to use #include <cstdio>, and cannot use recursion.
I'm trying to get the bottom sentence with quotes around it:
What number shall I start with? 8
The hailstone sequence starting at 8 is:
8 4 2 1
The length of the sequence is 4.
The largest number in the sequence is 8.
The longest hailstone sequence starting with a number up to 8 has length 17
"The longest hailstone sequence starting with a number up to 8 begins with 7"
I've gotten all of the above sentences coded except for the last one. Below is the code I have for the second to last sentence that determines the largest length. Also, I have no code yet for the last sentence.
<code>
int largestLength(int n)
{
if (n == 1)
{
return 1;
}
else
{
return max(length(n),largestLength(length(n)));
}
}
</code>
If you're able to find the length of the longest sequence then you must have found the starting point of that sequence. That's the value that you want to return.