Recursive function for Collatz

I have to design a recursive code for the collatz coefficient. I feel like i am very close but I cannot seem to call the collatz function correctly.

#include <iostream>
int collatz (int start, int steps=0){
if (start==1){
return steps;
}
else if(start%2!=0) {
steps=++steps;
return collatz(3*start+1);
}
else {
steps=++steps;
return collatz(start/2);
}

}

int main(int argc, char** argv)
{
int steps, start;
std::cout<<"Please enter number\n";
std::cin>>start;

std::cout<<collatz(start,steps)<<"\n";

}


Can anybody see what is wrong? The code has to print all of the numbers the code works through as well as the amount of steps
Last edited on
Topic archived. No new replies allowed.