# Correct algorithm, wrong result (cout problem?)

• 11-02-2009
dember
Correct algorithm, wrong result (cout problem?)
Hi everyone,

I am a C++ newbie. Here is the code (3n+1 algorithm) I am having problem with. No matter what the input number is (to the alg function), the result is always the same (namely, 16384). The algorithm seems correct to me, I guess the problem is with the cout in the main function. I don't know how to fix it. THX in advance.

Code:

```#include <iostream> int alg(int, int); int main() {   using namespace std;     int result = alg(300,1);     cout << result;     cin.get(); } //counter = 1 int alg(int n, int counter) {   using namespace std;   if (n == 1)     return counter;   else {     if (n % 2 == 1)       alg(3*n+1, ++counter);     else       alg(n/2, ++counter);   } }```
• 11-02-2009
EVOEx
It's a horrible algorithm, no offence. No need to do this recursively at all.

But the mistake is that you don't have a return statement for "alg" for every path of execution. That's all I'm going to say.
• 11-02-2009
anon
My compiler produces a warning:

Quote:

"ComeauTest.c", line 27: warning: missing return statement at end of non-void
function "alg"
}
^

• 11-02-2009
dember
Quote:

Originally Posted by EVOEx
It's a horrible algorithm, no offence. No need to do this recursively at all.

But the mistake is that you don't have a return statement for "alg" for every path of execution. That's all I'm going to say.

You are right, it can be done with a simple for loop :-D
However, are you implying that basically every function that has no return statement for every path of execution will output something crazy even if there is an actual return value? If so, why is that?
And just how does my complier know that there is no return statement for every path of execution for this specific algorithm, when in fact the Collatz conjecture seems to be true? Or has my computer found the exception? Lol.
• 11-02-2009
anon
Code:

```int alg(int n, int counter) {   using namespace std;   if (n == 1)     return counter;   else {     if (n % 2 == 1)       alg(3*n+1, ++counter);     else       alg(n/2, ++counter);   } }```
The bold part is the only part of the function that returns something meaningful. If you don't return anything from other parts of the function, whatever happens to be at a particular spot on the stack gets used as the "result".

And it should be pretty easy for the compiler to see, that the if branch returns something whereas the else part doesn't. Turn on warning diagnostics (e.g -Wall for GCC)

What it should be:

Code:

```int alg(int n, int counter) {   using namespace std;   if (n == 1)     return counter;   else {     if (n % 2 == 1)       return alg(3*n+1, ++counter);     else       return alg(n/2, ++counter);   } }```
Now you can easily see that each execution path through the function ends with a return statement.
• 11-02-2009
dember
Ok, I get it now. Thank you, anon.
• 11-02-2009
brewbuck
Quote:

Originally Posted by dember
However, are you implying that basically every function that has no return statement for every path of execution will output something crazy even if there is an actual return value? If so, why is that?

If there is no return value on every possible path, how does the compiler know what should be returned? Voodoo magic?