Hi. I have this very weird problem. The function getOps in the code below always returns 0, although it should be returning the ops value which gets calculated correctly.
int ops;
int getOps(int n, int m)
{
if(m == n)
return ops;
else if(m - 2 <= n)
return ops++;
else if(m / 2 >= n)
{
if(m % 2 == 0)
{
ops++;
getOps(n, m / 2);
}
else
{
ops++;
getOps(n, m - 1);
}
}
else
return ops += (m - n) / 2 + (m - n) % 2;
}
int main()
{
int n, m;
cin >> n >> m;
cout << getOps(n, m) << "\n";
cout << ops;
system("pause");
return 0;
}
Try the debugging the program with input values n = 1 and m = 10, for example. As you can see the return value of getOps(n, m) (which always returns 0) is different from the value of ops (4 in this case), although the values should be the same since getOps returns ops. Whatever values you try the program with getOps always returns 0. I have no idea why this is so.