Help with a programming challenge
<p>I am trying to make the programming challanges from the book written by Steven S. Skiena and Miguel A. Revilla.</p>

<p>But I am already having problems with the first challenge called "The 3n+1 problem".</p>

<p>The challenge can be found here: <a href="http://www.programming-challenges.com/english/pdfs/110101.pdf" rel="nofollow">http://www.programming-challenges.com/english/pdfs/110101.pdf</a></p>

<p>The solution I though of is.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;

int CalcCycleLenght(int n)
{
    int t = 1;
    while (n != 1)
    {
        if (n % 2)
        {
            n = 3 * n + 1;
        }else{
            n = n / 2;
        }
        t++;
    }
    return t;
}

int main(int argc, char* argv[])
{
    int i;
    int j;
    vector&lt;int&gt; vi;
    vector&lt;int&gt; vj;
    vector&lt;int&gt; vm;

    int p = 0;

    while (scanf("%d %d", &amp;i, &amp;j) != EOF)
    {
        vi.push_back(i);
        vj.push_back(j);
        int max = 0;
        int n;
        for (int t = i; t &lt;= j; t++)
        {
            n = CalcCycleLenght(t);
            if (n &gt; max)
                max = n;
        }
        vm.push_back(max);
    }

    for (int i=0; i &lt; vi.size(); i++)
    {
        printf("%d %d %d \n", vi[i], vj[i], vm[i]);
    }   

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

<p>It compiles correctly (although I think I have one extra include) and the answers are correct according to the sample input and output. But the online judge says "wrong answer". I think my problem is the way I input or output the numbers.</p>

<p>[EDIT]</p>

<p>I Solved it. Seems that the problem was that sometimes i was bigger than j. I also removed the vectors and made the code more efficient. Here is the new main function.</p>

<pre><code>int main(int argc, char* argv[])
{
    int i;
    int j;

    int a;
    int b;

    int max;
    int p;

    while (scanf("%d %d", &amp;i, &amp;j) != EOF)
    {
        if (i &lt; j)
        { a = i; b = j; }
        else
        { a = j; b = i; }   

        max = 0;
        for (int t = a; t &lt;= b; t++)
        {           
            p = CalcCycleLenght(t);
            if (p &gt; max)
                max = p;
        }
        printf("%d %d %d\n",i,j,max);
    }    
    return 0;
}
</code></pre>

<p>Thanks</p>
