What is wrong with this C++ code? 3n+1 in programming-challenges/UVa
<p>The Programming-Challenges website marked it as a wrong answer. I checked with sample inputs and they were all correct. I added an optimization to the code, I made it so it doesn't check numbers that are known to be in another number's sequence, since it would be a subsequence and obviously have a shorter cycle length. </p>

<p>Also I just got back into programming so the program isn't too terse but I hope it is readable.</p>

<p>Here is the code:</p>

<pre><code>#include &lt;iostream&gt;
#inclue &lt;vector&gt;
struct record
{
   int number;
   int cyclelength;
};

void GetOutput(int BEGIN, int END)
{
    //determines the output order at the end of function
    bool reversed = false;
    if (BEGIN &gt; END)
    {
        reversed = true;
        int temp = BEGIN;
        BEGIN = END;
        END = temp;
    }
    vector&lt;record&gt; records;
    for (int i = BEGIN; i &lt;= END; ++i)
    {
        //record to be added to records
        record r;
        r.number = i;
        r.cyclelength = 1;
        records.push_back(r);
    }

    int maxCycleLength = 1;
    //Determine cycle length of each number, and get the maximum cycle length
    for (int i =0;i != records.size(); ++i)
    {
        //
        record curRecord = records[i];
        //ABCD: If a number is in another number's sequence, it has a lower cycle length and do not need to be calculated,
        //set its cyclelength to 0 to mark that it can be skipped
        if (curRecord.cyclelength != 0)
        {
            //
            while (curRecord.number != 1)
            {
                //next number in the sequence
                int nextNumber;
                //finds the next number
                if (curRecord.number % 2 == 0)
                    nextNumber = curRecord.number / 2;
                else
                {
                    nextNumber = curRecord.number * 3 + 1;
                    //if nextNumber is within bounds of input, mark that number as skippable; see ABCD
                    if (nextNumber &lt;= END)
                    {
                        records[nextNumber - BEGIN].cyclelength = 0;
                    }
                }
                curRecord.number = nextNumber;
                curRecord.cyclelength += 1;
            }
            maxCycleLength = max(curRecord.cyclelength, maxCycleLength);
        }
    }
    if (reversed)
    {
        cout &lt;&lt; END &lt;&lt; " " &lt;&lt; BEGIN &lt;&lt; " " &lt;&lt; maxCycleLength;
    }
    else
    {
        cout &lt;&lt; BEGIN &lt;&lt; " " &lt;&lt; END &lt;&lt; " " &lt;&lt; maxCycleLength;
    }
}

int main(){
    //The first and last numbers
    vector&lt; vector&lt;int&gt; &gt; input;
    int begin, end;

    while (cin &gt;&gt; begin &gt;&gt; end)
    {
        //storage for line of input
        vector&lt;int&gt; i;
        i.push_back(begin);
        i.push_back(end);
        input.push_back(i);
    }


    for (int i = 0;i != input.size(); ++i)
    {
        GetOutput(input[i][0], input[i][1]);
        cout &lt;&lt; endl;
    }

    return 0;
}
</code></pre>
