3n+1 problem

this is the link for the problem " http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36 "

this is my solution:
#include<iostream>
using namespace std;

int calc(int);

int main()
{
int i,j,A,B,anynum,
max=0;

cin>>i;
cin>>j;
if(i>j)
{
B=i;
A=j;
}
else if(j>i)
{
A=i;
B=j;
}

for(int nm=A;nm<=B;nm++)
{
anynum=calc(nm);
if(anynum>max)
{
max=anynum;
}
}

cout<<A<<" "<<B<<" "<<max;
return 0;
}
int num=0;
int calc(int a)
{

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


where did I go wrong?
calc is an int function. When you call calc recursively, what happens to the result of calc when it returns? Also note that after calc returns, you fall through to the bottom of calc without returning a value. Your compiler should have flagged this as a problem.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Topic archived. No new replies allowed.