Get the longest sequence in hailstone program
The following is a part of a hailstone cpp program.
I want to write an option to let users enter 2 numbers and the program evaluates the longest sequence the number has within the 2-number range and return the count.
I have been struggling it for hours :P but the program is not working I don't know why, may I ask whats wrong with it?
Thanks for any help in advance.
Code:
int longestHailstoneSequenceLength(int start, int end)
{
int max = 0;
int s= start;
while (s!=1)
{
int count = 0;
for (int s; s<(end+1); s++)
{
int i= count;
for (i = 2; i<1000; i++)
{
if (s%2==0)
{
s=s/2;
}else
{
s=(3*s)+1;
}
if (s==1) break;
if (max<count)
{
max=count;
}
}
}
}
return max;
}
Re: Get the longest sequence in hailstone program
Code:
int s= start;
while (s!=1)
...
for (int s; s<(end+1); s++)
The s used within the for loop is a different s to that used in the outer while loop. The s in the while loop is set to start and is never changed! The s used in the for loop is not initialised so starts with some random value.
Re: Get the longest sequence in hailstone program
i changed the for loop like this:
Code:
for (s=start; s<(end+1); s++)
{
int i= count;
for (i = 2; i<1000; i++)
{
however its still not working :((, i initialised the s to be same as start, and start with a loop counting sequence for one number first and the outer for loop goes from "start" till the "end" number, and change the max as count whenever it meets a number larger than max, and finally return the max
is the logic alright or did i miss any steps its not working?
Re: Get the longest sequence in hailstone program
Quote:
is the logic alright or did i miss any steps its not working?
Your logic isn't right. Have you tried stepping through the code using the debugger to see where it departs from what expected? Is this a homework assignment?
If you just want the max number of the hailstone sequences, the function only needs about 6 lines of code.
Re: Get the longest sequence in hailstone program
i tried printing out the numbers when its running and found it keep repeating printing "1" out nonstop I have to stop it manually...Im not sure which part went wrong but Im sure it never run the number after "start" number :/
i modified a bit for s but still not working:
Code:
int s= start;
while (s!=1)
{
int count = 0;
for (s=start; s<(end+1); s++)
{
int i= count;
for (i = 2; i<1000; i++)
{
:blush: I am a beginner in programming n shame Im still not that familiar xp
Yes I want the max only, so should I use a sequence to store or how should I modify?
I want to understand the concept behind xp
Re: Get the longest sequence in hailstone program
Try this
Code:
int longestHailstoneSequenceLength(int start, int end)
{
int max = 1;
for (int s = start; s <= end; s++)
for (int count = 1, ss = s; ss > 1; ss = (ss % 2) ? (3 * ss) + 1 : ss / 2, max = (++count > max) ? count : max);
return max;
}
If you don't follow the code then see
http://www.learncpp.com/cpp-tutorial...-if-operators/
which explains use of the , ? and :
Re: Get the longest sequence in hailstone program
it worksss :) couldn't imagine code could be that short :P still have a lot to learn
cuz Im using Xcode the first few times i didn't know where I can sort out my bug is except the formatting xp
Re: Get the longest sequence in hailstone program
so...the increment in the for loop is count ++, and in terms of if-else is it like:
Code:
for (int count=2, ss=s;ss>1;count++)
{
if (ss%2==0)
{
ss=ss/2;
}else
{
ss=3*ss+1;
}
if (max<count)
{
max=count;
}
}
return max;
Thanks so much :D
Re: Get the longest sequence in hailstone program
Quote:
Originally Posted by
iikitty
Code:
for (int count=2, ss=s;ss>1;count++)
Get into the habbit of writing PREincrements (++count) rather than post increments (count++).
The only time you want a post-increment is when you want the exact behaviour the post increment gives you. If you don't know/understand the difference (yet), then use pre-increments.
the same is true for decrements, use --count, unless you specifically want the behaviour of count--.
No I'm not going to explain. Just get into the habbit of doing this. It'll make your life a lot easier learning to do this NOW, rather than weening you off the post-inc/decrement later on.