This recursive function is intended to print all odd numbers starting with the number passed to it. It does that but then, it prints out odd numbers starting with 429496295, and decreasing after that until memory is full or something. The only way I knew it was actually doing what I want it to do plus these other numbers is by hitting pause/break immediately after allowing it to run.
How am I accessing these crazy numbers?
How am I accessing these crazy numbers?
Code:
#include <iostream>
using namespace std;
unsigned int odd(unsigned int n);
int main()
{
odd(20);
return 0;
}
unsigned int odd(unsigned int n)
{
if(n==0)
{
return 1;
} //end if
if((n%2)==0)
{
return odd(n-1);
} // end if
else
{
cout << n << ", " ;
return odd(n-2);
} //end else
} //end odd()
Comment