I am posting my solution for the 3n+1 problem on the online judge webpage. The judge seems to give the verdict of "wrong answer" even though the program works for the test cases given in the problem statement.
http://uva.onlinejudge.org/index.php...lem&problem=36
I would be glad if anyone would point out the errors.
Code:
#include <stdio.h>
int cycle_length(int n, int i)
{ int a;
if (n == 1)
return i;
if(n%2 == 0)
{a = cycle_length(n/2, ++i); return a;}
else
{a = cycle_length((3*n+1), ++i); return a;}
}
int main()
{
int a, b, i, max, min;
scanf("%d", &a);
scanf("%d", &b);
if (a >= b)
{max = a; min = b;}
else
{max = b; min = a;}
int c[max - min +1];
for(i = 0; i < (max-min+1); i++)
{
c[i] = cycle_length((min+i), 1);
}
for (i=1; i <(max-min+1); i++)
{
if (c[i] >= c[0])
{int t = c[0]; c[0] = c[i]; c[i] = t;}
}
printf("%d %d %d", a, b, c[0]);
return 0;
}