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.

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;
}

Recommended Answers

All 8 Replies

I have no idea what your code is trying to do.

the longest sequence the number has within the 2-number range

What does that mean? The entire set of numbers is a sequence, and every subset of the numbers is a sequence.

There are two inputs from user "start" and "end", and within the range,
each interger will be evaluated its length of hailstone sequence (so in the programe i used "i" to record how many times the calculation processed until the number equals 1)
I am writing this part to find out the longest length of sequence of the number within the range.

but I am not sure if I should use an array or continue with this... :(

For other people like me who are not psychic, this is the actual question:

Given a range of integers (start, end), take each integer and subject it to the following step, repeatedly, until it becomes the number "1":

If it's even, divide by two.
If it's odd, multiply by three and add one.

Count how many times you execute the step.

When you're done, you'll have one count for each number in your range. What is the highest count?

Anyway, problems with your code that jump out:

You're creating two separate variables named s. They're different variables. Give them different names. If they're meant to be the same variable, don't create them twice.

This loop: while (s!=1) will never end because that s variable never changes.

Your variable count never changes. If it's meant to be counting something, should you be increasing it sometimes?

On line 11 you set i to be the same as count. The very next thing you do with i is set it to 2, on the next line. Why are you setting it to be the same as count?

Yes exactly thanks
how should I work on this should I use array instead? xPP

Right I found it keep repeating printing "1" out nonstop I have to stop it manually :/ and I modified a bit: (but still not working :( )
I used "i" throught out the part instead without using count, and put the "if (s==1) break;" right after second for loop but seems its not even run till there xp

int longestHailstoneSequenceLength(int start, int end)
{
    int max = 0;
    int s= start;
        for (s=start; s<(end+1); s++)
        {
            for (i = 2; i<1000; i++)
            {
                if (s==1) break;
                if (s%2==0)
                {
                    s=s/2;
                }else
                {
                    s=(3*s)+1;
                }
                cout<<s<<"\t";
            }            
        }
    if (max<i)
    {
        max=i;
    }    
    cout<<"end"<<endl;
    return max;
}

Your for loop is meant to go from start to end, right? Your for loop is meant to go:

s, s+1, s+2, s+3 ...end

But you change s every time inside the for loop, to 1. So every time you go round the for loop, s is back to 1. It will never reach end.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.