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.

Recommended Answers

All 12 Replies

Try changing:
ops++; to ops += 1;

Try changing:
ops++; to ops += 1;

What difference does it make? ops++ is identical to ops += 1 which is indentical to ops = ops + 1. I still tried though, but the problem remains.

I do not get the behaviour that you describe. For input values of n = 1 and m = 10, getOps() returns 3 and ops is set to 4. This is because, in this case the getOps() returns from line 8. Since you're using ops++ the value of ops is returned before ops is incremented. If you change line 8 to

return ++ops;

then getOps() returns the value of ops properly.

Also, do you have a good reason for making ops a global variable? If not you should declare it in main() and pass it by reference to getOps() .

Just tried the original code in the compiler with example 1 and 10 and the result 3 and 4?

I do not get the behaviour that you describe. For input values of n = 1 and m = 10, getOps() returns 3 and ops is set to 4. This is because, in this case the getOps() returns from line 8. Since you're using ops++ the value of ops is returned before ops is incremented. If you change line 8 to

return ++ops;

then getOps() returns the value of ops properly.

Also, do you have a good reason for making ops a global variable? If not you should declare it in main() and pass it by reference to getOps() .

That's not true. The return value always gets evaluated before it's returned. Nevertheless, I tried what you suggested and it still doesn't work.

@ravenous
valid point about the global.

@moni94
Is the correct output 3 and 4? Because that's what I'm getting.

edit: just corrected using ++ops and now I get 4 and 4.

@moni94
Is the correct output 3 and 4? Because that's what I'm getting.

I suspect 4 and 4 is the desired output. You get this if you use ++ops

If the "if" statement that is the subject of the else in line 9 yields true in its test, then the function falls off the end without executing a return statement. The value that the function returns in that case is undefined.

@ravenous
valid point about the global.

@moni94
Is the correct output 3 and 4? Because that's what I'm getting.

edit: just corrected using ++ops and now I get 4 and 4.

We'll I'm getting 0 and 4 in both cases, while I should be getting 4 and 4. This is strange.

As for the global var, I don't see what's bad about it. The function is recursive and I don't see the point in passing an extra parameter every time.
Besides I wouldn't be able to demonstrate the problem if it wasn't global.

If the "if" statement that is the subject of the else in line 9 yields true in its test, then the function falls off the end without executing a return statement. The value that the function returns in that case is undefined.

Yeah but the function is recursive and is supposed to call itself until eventually it returns something.

Yeah but the function is recursive and is supposed to call itself until eventually it returns something.

Yeah but you're doing it wrong.

Every call, including recursive calls, must have a matching return. If any of these calls causes the "if" test in line 9 to be true, the corresponding return causes undefined behavior. The entire program could potentially do anything at all after that.

I really don't want to debate this. The code is broken, and there is no point in looking for any other problems until this one is fixed.

Yeah but you're doing it wrong.

Every call, including recursive calls, must have a matching return. If any of these calls causes the "if" test in line 9 to be true, the corresponding return causes undefined behavior. The entire program could potentially do anything at all after that.

I really don't want to debate this. The code is broken, and there is no point in looking for any other problems until this one is fixed.

You are totally right. Here's how I fixed it:

int ops;

int getOps(long long n, long long m)
{
	if(m == n)
		return ops;
	else if(m - 2 <= n)
		return ++ops;
	else if(m / 2 >= n)
	{
		if(m % 2 == 0)
		{
			ops++;
			return getOps(n, m / 2);
		}
		else
		{
			ops++;
			return getOps(n, m - 1);
		}
	}
	else
		return ops += (m - n) / 2 + (m - n) % 2;	
}

Now it returns a proper value. Thanks.
I hope I won't make dumb mistakes like this any longer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.