Good afternoon everyone,

I'm currently working towards to provide the following output for my code:

What number shall i start with? 7
The hailstone sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is 17.
The longest hailstone sequence starting with a number up to 7 has length 17
The longest hailstone sequence starting with a number up to 7 begins with 7

Below is what I've completed thus far:

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

//Function Variable Declaration.
int lengthof_Sequence(int x), max_Sequence (int m), hailstone_Sequence (int n), count, length;

int main(int argc, char** argv)
{
    int user_Input, max_Input; 

    cout << "What number shall I start with? "; cin >> user_Input; cout << user_Input << endl;

    cout << "\nThe length of the sequence is " << hailstone_Sequence (user_Input) << ".";

    cout << "\nThe largest number in the sequence is: \n";

    cout << "The longest hailstone sequence starting " << user_Input << " with a number up to  has length " << count<< "." <<endl;

    cout << "The longest hailstone sequence starting with a number up to " << user_Input << " begins with " << user_Input;
    return 0;
}

    /* The hailstone sequence takes 'n' that is greater than 1 from the user. If
    // 'n' is even, computes n/2. If n is odd, computes 3n+1. Both are done till n = 1
    // is reached.
    */
    int hailstone_Sequence (int n)
    {
        int count = 1, length = 0;

        if (n > 1)
        {
            cout << "The hailstone of sequence starting at "  << n << " is: " << endl;
            cout << n << " ";

            while (n > 1)
            {

                if ((n % 2) == 0 )
                {
                   n = n / 2;
                }
                else
                {
                    n = 3 * n + 1;
                }
                cout << n << " "; 

                count++;

            }
            length = count;
        }
           return count;
    }

    /* Lengthof_Sequence 
    // returns *pending* 
    //
    */
    int lengthof_Sequence(int x)
    {

    }

    int max_Sequence (int m)  
    {

    }  

I'm having trouble coming up with a way to print the max and this line "The longest hailstone sequence starting 7 with a number up to has length____." for the output. Does anyone have any advice or suggestions?

Recommended Answers

All 48 Replies

@rproffitt

I have spent days getting to where I am with the current code above. I would like to learn how to overcome my current issue without copying a completed program. Appreicate your response though nontheless.

v/r

commented: Good for you! Hope we can help. +15

I am missing where you are stuck here. That is, your problem statement is a tad unclear.

Try this. Take the problem and break it down to discrete steps that you would do manually. Code each step and step towards a full solution.

I'm having trouble coming up with a way to print the max and this line "The longest hailstone sequence starting 7 with a number up to has length____." for the output.

You don't explain exactly where you are stuck, but all you need is something like

for i = 1 .. 7   (pseudocode)
       length = hailstone_Sequence (i)
       keep note of largest length found so far
print "The longest hailstone sequence starting with a number up to has 7 length " + largest length
commented: That's a start. +11

Update

Here is output for the code I provided in my original question:

What number shall I start with? 7
The hailstone of sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The lenght of the sequence is 17.
The largest number in the sequence is: (Problem line)
The longest hailstone sequence starting 7 with a number up to has length 0. (Problem line)
The longest hailstone sequence starting with a number up to 7 begins with 7

I thought I was getting somewhere until I ran into these problem spots. Now I'm really confused on how to generate the desired output. I'm thinking about coding an array, the size of the array will be depended on the calcuations from the hailstone_Sequence (which I would make into a void function). Do you all believe my current reach is plausible?

v/r

The "Problem line" is waiting for you to write code to find the answer. If you don't know the algorithm then that's what you research next.

What no one pointed out is rarely do folk write your code. They may read and see where there's an issue but here, your code is missing.

The largest number in the sequence is: (Problem line)

In general... you have a loop that is generating a sequence of values and you want to know the largest one

largest so far = 0
start loop
    if value > largest so far
         largest so far = value
 end loop
 largest so far now contains largest value from the sequence

Update Number 2

I decided to scrap everything and start over. I'm going by function to function.

Current Issue is with function hailstone_Sequence(int n)
I'm currently getting a complication error for my output. The program doesn't highlight where the issue is coming from, but with my lack of experience, the issue isn't obvious for me. Below is my current code:

#include <iostream>
#include <cstdio>
using namespace std;

 //Function Prototype Declaration
 int next(int);

 void hailstone_Sequence(int);

 int main (int argc, char** argv)
{
    int user_Input, temp = 0;

    cout << "What number would you like to start off with is? "; cin >> user_Input; cout << user_Input << "\n";

    cout << "Your hailstone_Sequence is as follows: " << endl; 
    cout << hailstone_Sequence(user_Input);      
}

 /* The hailstone sequence computes only if 'n' is greater than 1 from the user. If
 // 'n' is even, computes n/2. If n is odd, computes 3n+1. Returns a number
 // that follows 'n' in the sequence. 
 */
 int next(int n)
    {
        if (n > 1)
        {
            while (n != 1)
            {

                if ((n % 2) == 0)
                {
                  // cout << n << " "; 
                   n = n / 2;
                   return n;
                }
                else
                {
                    n = 3 * n + 1;
                    return n;
                }
                //cout << n << " "; 
            }
        }
    }

    /* Hailstone_Seuence(int n)
    // will take 'n' and will output the entire
    // sequence beginning at 'n'.
    */
    void hailstone_Sequence (int n)
    {
        int count, value;

        cout << n << " ";

        value = next(n); 

        for (count = 0; value >= 1; count++)
        {
            value =  next(value);

            cout << value << " ";

        }  
    }

More information on function hailstone_Sequence(int n)
The function is suppose to do the following:

- Takes an integer 'n' and writes the entire hailstone sequence starting at 'n'

The original question at the start of my post is still the desired output overall. I'm trying to tackle the problem at a different angle.
Any help would be much apprecated.

I'll be working on this till my night class at 1700.

What does the function next return if n <= 1?
Have it return 0 and then you can test for zero in your count loop.
Something like:

void hailstone_Sequence(int n)
{
    int count, value = n;

    for (count = 0; ; count++)
    {
        if (value <= 0)
            break;

        std::cout << value << " ";

        value = next(value);
    }
    std::cout << "count: " << count << std::endl;
}

Function next... is this supposed to return the single next number in the sequence? If so it doesn't need a while loop - it's only calculating one value. What happens in your existing code is that it always encounters one or the other return, so the loop never enters a second iteration.
ALso, since next is a private internal function its not the place to test for invalid input (n<=0). You should test for that just once, before starting to make repeated calls to next

This leaves you with a much simpler structure

private next(n) 
    if n is even return ...
    else return ...

public hailstone(n) 
  loop
       update output, length, whatever
       n = next(n)
       if n=1 return

main
    get starting value for n
    reject/retry value if n <= 0
    hailstone(n)

@JamesCherrill

I was able to come up with the following code yesterday afternoon in the library, what do you think?

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

 //Function Prototype Declaration
 int next(int), sequence_Lenght(int), sequence_Max(int);

 void hailstone_Sequence(int);

 int main (int argc, char** argv)
{
    int user_Input, temp = 0;

    cout << "What number would you like to start off with is? "; cin >> user_Input; cout << user_Input << "\n";

    cout << "Your hailstone_Sequence starting at " << user_Input << " is: " << endl; 
    hailstone_Sequence(user_Input);

    cout << "\nThe lenght of the hailstone sequence is " << sequence_Lenght(user_Input); cout << ".\n";

    cout << "The largest number in the sequence is ";

}

    /* 
    // The hailstone sequence computes only if 'n' is greater than 1 from the user. If
    // 'n' is even, computes n/2. If n is odd, computes 3n+1. Returns a number
    // that follows 'n' in the sequence. 
    */
    int next(int n)
    {
        if (n > 1)
        {

            if ((n % 2) == 0)
            {
                n = n / 2;
                return n;
            }
            else
            {
                n = 3 * n + 1;
                return n;
            }
        }
    }

    /* 
    // Hailstone_Seuence(int n)
    // will take 'n' and will output the entire
    // sequence beginning at 'n'.
    */
    void hailstone_Sequence (int n)
    {
        int value = 0, temp = 0;

        cout << n << " ";

        while (temp != 1)
        {
            value = next(n);
            temp = value;

            cout << temp << " ";
            n = temp;
        }

    }

    /*
    // Sequence_Length(int n)
    // Takes an integer 'n' and returns the output length of
    // the hailstone sequence. 
    */
    int sequence_Lenght(int n)
    {
        int count, value = 0, temp = 0;

        for (count = 1; temp != 1; count++)
        {
            value = next(n);
            temp = value;

            n = temp;
        }
        return count;
    }

    //int sequence_Max(int n) *Pending*
    //{

    //}

I'm currently working on how to create a function that returns the max number from the sequence.

Hi
Firstly, I have to confess that C++ is not a language I know well, so I'll confine myself to the logic which would look much the same in Java, Swift, C# etc.

That code looks OK to me. There are many small redundacies (eg lines 82-85), but they are nothing to worry about and you will get better at that as you progress. More importantly the way you have packaged the code into functions makes the program easy to read and understand, and keeps any redundancy sensibly low.

I'm not saying that's how I would do it, nor am I saying its the best way, but it's perfectly acceptable from someone who is still learning the basics. With that code as a template and the hints I posted earlier you should have no difficulty with the max number.

Here is my max function. The was pretty tricky, but writing it out helped a lot.

  /* 
    // Sequence_Max(int n)
    // Takes an integer 'n' and returns the largest value in the 
    // hailstone sequence starting at 'n'.
    */
    int sequence_Max(int n)  
    {
        int max = 0, temp1 = 2, temp2;

        temp2 = next(n);

        while (temp1 > 1)
        {

            if (n > temp2)
            {
                max = n;
                temp2 = next(temp2);
                temp1 = temp2;

            }
            else if (n < temp2)
            {
                max = temp2;
                temp2 = next(temp2);
                temp1 = n;
                n = max;
            }

        }
        return max;
    }

I'm sorry to say that me eyes crossed when I tried to read that code! It's way too complex. and the temp variables are not needed.
Looking at all your code I see a lot of unneccessary temp variables, and I wonder if there's some misunderstanding that's causing you to think that you need them?
For example

    void hailstone_Sequence (int n)
    {
        int value = 0, temp = 0;
        cout << n << " ";
        while (temp != 1)
        {
            value = next(n);
            temp = value;
            cout << temp << " ";
            n = temp;
        }
    }

doesn't need those variables, this will do:

    void hailstone_Sequence (int n)
    {
        cout << n << " ";
        while (n != 1)
        {
            n = next(n);
            cout << n << " ";
        }
    }

It's way too complex. and the temp variables are not needed.

Agree. The extra variables detract from the readability and the logic. Temporary variables are used when you can't simply overwrite a variable and be done with it. You need to keep its former value for whatever reason because you may need it later. Swapping values is an example. If x is 5 and y is 8 and I want to end up with x is 8 and y is 5, I need a temporary variable because

x = y;
y = x;

won't work because I lost the value 5 after the first line so I have no way to assign y to equal 5 in the second line. To make it work, I need a temp variable.

int temp = x;
x = y;
y = temp;

In YOUR program, there's no need to "remember" any past values as far as I can see, so writing over the old value with the new value poses no problem, so no need for a temp variable.

Now regarding your maximum number, again, I see no reason for a temporary variable. If I have a new maximum value, then I want to assign max to that new value. I couldn't care less what the OLD max value was, so no need to store it. I'm expecting to see some code that looks like this in your max function...

if(n > max)
{
    max = n;
}

but I'm not seeing it anywhere.

What I AM seeing is a loop where max gets assigned a value every time you go through the loop, which seems wrong since once n is a power of 2, it should steadily decrease down to 1 and max should not be changed, yes?

I didn't run your code. You posted it, but did not mention whether it gives correct results and you were done with the program and needed no more help or whether there were still stuck.

@JamesCherrill
Thank you for that review. At the time back when I was working soley on that function, I believe I was looking to have it do more that one thing. For instance, to not just print out the sequence, but to also print out the length of the sequence. I have trimed the fat out of that funcition. It ran successfully with the fat in there previously.

@AssertNull
The max function compares each number within the list of the sequence to determine the max. Currenlty the program works. Here is a link to my code:

Click Here

If I can improve on the function, I'm open to learn.

Currenlty the program works.

What values did you enter that worked?

Often new programmers test their programs with numbers like 19, 23, and 29, see that those values produce good results, and pronounce the program good to go. Indeed it appears to work for these values.

However, Computer Science professors can be quite mischievious and their idea of fair play may not be yours. I noticed this comment in your code...

The hailstone sequence computes only if 'n' is greater than 1 from the user.

Surfing the net, I see no rule for the Hailstone Sequence that entering 1 as input isn't legitimate. As far as I can tell, any positive integer is fair game for input. Your program should handle 1. It's the most trivial input of all, yet it's the value that breaks your program. You check to see if the function is passed 1. If it is, you do not do anything. However, your function is supposed to return an integer. It seems like if it is passed 1, it should immediately return 1. Does it?

And if I should only enter a value greater than 1 for your program to work, I should be told that in thee prompt. Perhaps use this as a prompt...

Please enter a positive integer greater than 1

Again, I believe you need to handle 1. It is legitimate input. I'm also a big fan of testing the input to make sure it is good, and if not, telling the user what the problem is and giving them another chance.

    do
    {
        cout << "Please enter a positive integer: ";
        cin >> user_Input;
        if(user_Input < 1)
        {
            cout << user_Input << " is not a positive integer.  Please try again.\n";
        }
    }
    while(user_Input < 1);

Take another look at the other functions because they still contain a lot of fat just like you removed from the first fn. Otherwize it's OK.

Purely to help learning, here's some working code that shows a slightly different way to look at it, while showing how much fat there still is. (The code is Java, beause that's my bag, but at this level it's the same as C++ apart from the print statements.
First - a fn to generate the sequence:

    // generates Hailstone sequence with initial value = start, 
    // calls analyser for each value in the sequence
    void generateSequence(int start) {
        int n = start;
        while (true) {
            analyseValue(n);
            if (n <= 1) return;
            n = ((n % 2) == 0) ? n / 2 : n * 3 + 1;
        }
    }

All it does is generate the sequence, but as it generates each value it passes it to a separate analysis fn. To me this seems a good way to split the functionality because you can change that to perform any other analysis withot needing to do anything to the code that generates the sequence (separation of responsibilities).
This analysis fn prints each value, calcs the length of the sequence, and records the max value in the sequence...

    int length = 0, max = 0;

    void analyseValue(int value) {
        // get passed each value in the sequence for analysis
        System.out.print(value + " ");
        length++;
        if (value > max) max = value;
    }

So all that remains is to run a test case and print the final results...

    void test() {
        generateSequence(7);
        System.out.println("\nlength =  " + length + "  Max = " + max);
    }

I'm not suggesting you should change your code to look like that, but just see how that was done and take the lessons back to your own code and eliminate the redundant variables. For what it's worth that's less than 20 lines of code total.

"The longest hailstone sequence starting with a number up to ..." is just a little more complicated, but should still only require about a dozen lines (including brackets!).

I just went through your code and did a few pretty minor changes handling the base case of 1 and getting rid of the temporary variables. You do not have to do much. You are definitely on the right track.

I think you've checked off the "show effort" box and sometimes the best way to learn is to see some completed revised code. I'm going to post that revised code in this thread. However, in case you want to try to work it out on your own, I'm going to delay doing that. I'm going to send you a PM with the revised code. If you don't want to see it and instead want to try to figure it out on your own, simply don't open the PM till you're ready. I'll post it in this this thread for posterity in a few days or when you say it's OK, whichever comes first.

Again, not too many changes required and I think you'll see it's more readable.

Code for anyone interested.

#include <iostream>
using namespace std;

 //Function Prototype Declaration
 int next(int), sequence_Length(int);

 void hailstone_Sequence(int);

 int sequence_Max(int);

 int main (int argc, char** argv)
{
    int user_Input;

    do
    {
        cout << "Please enter a positive integer: ";
        cin >> user_Input;
        if(user_Input < 1)
        {
            cout << user_Input << " is not a positive integer.  Please try again.\n";
        }
    }
    while(user_Input < 1);

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

    cout << "The largest number in the sequence is " << sequence_Max(user_Input); cout << ".\n";
    //cout << "The longest hailstone sequence starting with a number up to " << user_Input << "has length " << << ".\n";

}

    /* 
    // The hailstone sequence computes only if 'n' is greater than 1 from the user. If
    // 'n' is even, computes n/2. If n is odd, computes 3n+1. Returns a number
    // that follows 'n' in the sequence. 
    */
    int next(int n)
    {
        if (n <= 1)
        {
            return 1;
        }

        if ((n % 2) == 0)
        {
            n = n / 2;
            return n;
        }
        else
        {
            n = 3 * n + 1;
            return n;
        }
    }

    /* 
    // Hailstone_Sequence(int n)
    // will take 'n' and will output the entire
    // sequence beginning at 'n'.
    */
    void hailstone_Sequence (int n)
    {
        while(n > 1)
        {
            cout << n << " ";
            n = next(n);
        }
        cout << n << "\n";
    }

    /*
    // Sequence_Length(int n)
    // Takes an integer 'n' and returns the output length of
    // the hailstone sequence. 
    */
    int sequence_Length(int n)
    {
        int count;
        for (count = 1; n > 1; count++)
        {
            n = next(n);
        }
        return count;
    }

    /* 
    // Sequence_Max(int n)
    // Takes an integer 'n' and returns the largest value in the 
    // hailstone sequence starting at 'n'.
    */
    int sequence_Max(int n)  
    {
        int max = n;        
        while (n > 1)
        {
            n = next(n);
            if(n > max)
            {
                max = n;
            }
        }
        return max;
    }

@AssertNull awesome code. Looking at my code compared to yours....I definitely live up to the beginner title. I apologize for the late reply. I pm you about my other classes I was neglecting because I just wanted to program, but I did well to get back on track witht them. I worked on an extra function on Friday, I'll post my updated code in a few. I have one more function to write which I'm kinda stuck on.

Last stretch before code contracts reveiw

Good aftnoon everyone,

I'm currently stuck on the last function needed for this program. Below is the intended function discription:

Takes an integer and returns the start value of the longest hailstone sequence that starts on a value from 1 to n.

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

 //Function Prototype Declaration
 int next(int), sequence_Length(int), sequence_Max(int), sequence_Comparasion(int), 
     initialcomp_Num(int);

 void hailstone_Sequence(int);

 int main (int argc, char** argv)
{
    int user_Input, temp = 0;

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

    cout << "The largest number in the sequence is " << sequence_Max(user_Input); cout << ".\n";
    cout << "The longest hailstone sequence starting with a number up to " << user_Input << " has length " 
         << sequence_Comparasion(user_Input) << ".\n";

    cout << "The longest hailstone sequence starting with a number up to " << user_Input << " begins with "
         << initialcomp_Num(user_Input) << ".\n";

    //cout pending 
}

    /* 
    // The hailstone sequence computes only if 'n' is greater than 1 from the user. If
    // 'n' is even, computes n/2. If n is odd, computes 3n+1. Returns a number
    // that follows 'n' in the sequence. 
    */
    int next(int n)
    {
        if (n > 1)
        {
            if ((n % 2) == 0)
            {
                n = n / 2;
                return n;
            }
            else
            {
                n = 3 * n + 1;
                return n;
            }
        }
        else if (n <= 1)
        {
            return n;
        }
    }

    /* 
    // Hailstone_Sequence(int n)
    // will take 'n' and will output the entire
    // sequence beginning at 'n'.
    */
    void hailstone_Sequence (int n)
    {
        cout << n << " ";

        if (n > 1)
        {
            while (n != 1) //was changed from != to >=
            {
                n = next(n);
                cout << n << " ";
            }
        }  
    }

    /*
    // Sequence_Length(int n)
    // Takes an integer 'n' and returns the output length of
    // the hailstone sequence. 
    */
    int sequence_Length(int n)
    {
        int count, value = 0, temp = 0;
        if(n > 1)
        {
            for (count = 1; temp != 1; count++)//temp was changed from != to >=
            {
                value = next(n);
                temp = value;

                n = temp;
            }
            return count;
        }
        else if (n <= 1)
        {
            count = n;
            return n; 
        }

    }

    /* 
    // Sequence_Max(int n)
    // Takes an integer 'n' and returns the largest value in the 
    // hailstone sequence starting at 'n'.
    */
    int sequence_Max(int n)  
    {

        int max = 0, temp1 = 2, temp2;

        temp2 = next(n);

        if ( n <= 1)
        {
            return n;
        }

            while (temp1 > 1)
            {

                if (n > temp2)
                {
                    max = n;
                    temp2 = next(temp2);
                    temp1 = temp2;

                }
                else if (n < temp2)
                {
                    max = temp2;
                    temp2 = next(temp2);
                    temp1 = n;
                    n = max;
                }
            }
            return max;
    }

    /* 
    // Sequence_Comparasion(int n)
    // The function takes 'n' and compares the length of each integer
    // as if each was 'n' up to the first input. Returns the longest
    // sequence starting from 1 to 'n'.
    */
    int sequence_Comparasion(int n)
    {
        int temp1 = 0, count, top;

        if ( n <= 1)
        {
            return n;
        }

            top = sequence_Length(n);

            for (count = 1; count != n; count++)
            {
                if (top < sequence_Length(count))
                {

                    top = sequence_Length(count);
                }
            }  
            return top; 
    }

    /* 
    // initialcomp_Num(int n)
    // The function takes 'n' and returns the start value
    // of the longest sequence that starts on a value from
    // 1 to 'n'.
    */
    int initialcomp_Num(int n) *Pending*
    {
        int temp1 = 0, count, inital;

        //Function will work as intended if 'n' isn't less than 1.
        if ( n <= 1)
        {
            return n;
        }

            //Function's main calculation. 
            inital = sequence_Length(n);

            for (count = 1; count != n; count++)
            {
                if (inital < sequence_Length(count))
                {

                    inital = sequence_Length(count);
                }
            }  
            return inital; 
    }

It's like I'm still comparing the sequence lenght, but only in order to identify the integer with the largest lenght. I'm not allowed to use the previous function before it, but have been looking at that code to come up with a way around that. Each function's body can only have 15 noncomment lines, no more than that. Any suggestions?

So does your int sequence_Comparasion function work? If so, seems like a slight tweak of that function should make the other function (int initialcomp_Num) work. Use that function(int sequence_Comparasion) as a skeleton for the function you are stuck on and add a few lines of code.

You have a variable called top, which keeps track of the longest length. You need the start value that results in a sequence that is length top. So declare a value called start. Every time you assign a new value to top, assign the value that resulted in the sequence of length top to start.

Then return start instead of top.

@AssertNull

Currently the sequence_Comparasion function works. I'll see what I can do about the start variable.

Not sure what I'm doing wrong, I get this output:

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 0. (should be 7 not 8).

Here is the last function, am i missing something?

 int initialcomp_Num(int n)
{
    int temp1 = 0, count, inital, inital2, start = 0;

    //Function will work as intended if 'n' isn't less than 1.
    if ( n <= 1)
    {
        return n;
    }
    else
    {
        inital = sequence_Length(n);

        for (count = 1; count != n; count++)
        {
            if (inital < sequence_Length(count))
            {

                inital = sequence_Length(count);
                inital2 = inital;

                if (inital2 > sequence_Length(count))
                {
                    start = count;
                }
            }

        }  
        return start; 
    }
}

Is "inital" a typo and supposed to spell as "initial" or does it stand for something else? I kept the name as is.

No need for temp1 or inital2. They just add to the length and the complexity and increase the chance of mistakes.

As for your error, note that the function returns 0, which if you'll notice is what you initialized start to. Sometimes that's a coincidence, but sometimes it means that start never changed value when it was supposed to. Experiment by initializing start to equal something silly like -938. If it returns -938, it never changed. Now look at the code to see where start should change, so look at line 24. Does it ever execute? If not, why?

Again, get rid of the excess variables. temp1 is never even used. inital2 is used, but does it serve any purpose other than complicating things? Use as many variables as you need, but no more.

Oh wow, definitely a typo! I'll change that. I figured out what was wrong. Here is the code for the function below:

 int initialcomp_Num(int n)
    {
        int count, initial, start;

        //Function will work as intended if 'n' is not less than 1.
        if (n <= 1)
        {
            return n;
        }
        else
        {
            initial = sequence_Length(n);

            for (count = 1; count != n; count++)
            {
                if (initial < sequence_Length(count))
                {
                    initial = sequence_Length(count);
                    start = count;
                }
            }
            return start;
        }
    }

If you feel this isn't easy to read, then could you help with the overall look of the code, for instance, do you believe any programmer could come along and see this would be able to pick up what I'm tryin to accomplish here? I feel I may have made a mess of things, but I can only have 15 at the max noncomment lines for each body of my functions. I con't do more than one loop per function as well. With these requirements, do you feel I can clean this up a bit? I understand readable is a huge part of programming teams.

I assume the lines with only a bracket don't count as a line, right?

I entered 7 into your program and got this line...

What number shall I start with? 7
7
The hailstone sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is 17.
The largest number in the sequence is 52.
The longest hailstone sequence starting with a number up to 7 has length 17.
The longest hailstone sequence starting with a number up to 7 begins with 6422312.

When I initialize start to -938, I get -938 in the printout, so when n is 7, start never gets set to a reasonable value.

As far as readability, readability is fine except I don't understand why you name your variable initial rather than, say, max, or "top" as you had it before. You already have a "start" variable. The "start" value and the "initial" value seem to describe the same thing, so change "initial" to "max" or "top" like you had in the other function. Or if you like the word "initial", use that INSTEAD of "start". Whatever you use, use names that are descriptive.

Once you fix the bug of it not working for n=7, I imagine that any programmer will be able to read your code, provided you use some good variable names. The comment at the top of the function makes it clear.

A small point:
line 8 you return in the if construct, so the else and its associated brackets are redundant. This is a very common pattern (it's called a guard )...

myFunction(params) {
    if (there's something wrong with the parameters) 
          return errorvalue;

    // no need for an else here, just get on with it!

    do rest of function
}
commented: Good point +7
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.