Total beginner.
The exercise is to make a function that takes an input and if the input is even, a certain calculation is done. If odd, a calculation is done. Then the new value is reassigned and the sequence continues until 1 is reached.
Example: If the starting integer is 1, the return value should be 0, since it takes no steps to reach one. If the starting integer is 3, then the sequence would go: 3, 10, 5, 16, 8, 4, 2, 1, and the return value should be 7.
Been messing with this for hours and I keep getting a return of 0 from counter.
(The main() is used only for testing purposes)
Thanks
#include <iostream>
using namespace std;
int hailstone(int outinput);
int main()
{
int outinput=10, counter;
hailstone(outinput);
cout << counter << endl;
return 0;
}
int hailstone(int outinput)
{
int counter;
for(outinput; outinput == 1; )
{
if(outinput % 2 == 0)
{
outinput = outinput / 2;
counter++;
}
else if(outinput % 2 != 0)
{
outinput = outinput * 3 + 1;
counter++;
}
}
return counter;
}
Aucun commentaire:
Enregistrer un commentaire