|
-
October 4th, 2014, 05:01 AM
#1
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;
}
-
October 4th, 2014, 05:45 AM
#2
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.
Last edited by 2kaud; October 4th, 2014 at 05:47 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 4th, 2014, 06:06 AM
#3
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?
-
October 4th, 2014, 07:19 AM
#4
Re: Get the longest sequence in hailstone program
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.
Last edited by 2kaud; October 4th, 2014 at 07:22 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 4th, 2014, 07:35 AM
#5
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++)
{
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
-
October 4th, 2014, 07:42 AM
#6
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 :
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 4th, 2014, 08:10 AM
#7
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
-
October 4th, 2014, 09:49 AM
#8
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
-
October 6th, 2014, 06:47 AM
#9
Re: Get the longest sequence in hailstone program
 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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|