# counting steps

• 12-18-2006
muran_pling
counting steps
ok heres my problem..
1. if x is even, divide x by 2.
2. if x is odd, multiply x by 3 and add 1, and count 1 step.
3. Repeat steps 1 and 2 until you arrive at the number 1.

For example, if we begin with 13, the sequence we obtain is:
13, 40, 20, 10, 5, 16, 8, 4, 2, 1
with 2 steps (from 13 to 40 and from 5 to 16).

this is my code so far... cant seem to get the loop working but i got the even/odd going...

Code:

```#include <cstdlib> #include <iostream> using namespace std; int steps(int a); int main() {     int month;     cout<<"enter month: "<<endl;     cin>>month;         cout<<steps(month)<<endl;         system("PAUSE");     return EXIT_SUCCESS; } int steps(int a) {     int s = 1;     int count = 0;     int answer;         for (int x = 0; x>1; x++) <---- i have a feeling something is wrong here...     {       if (a%2==0)       {       answer = a/2;             }       else       {       answer = (a * 3) + 1;       count++;       cout<<answer<<endl;       }     }         return count; }```
• 12-18-2006
I BLcK I
use a do-while loop because you dont know how many iterations your going through, but with a for loop you do.

Code:

```do{       if (a%2==0)       {answer = a/2;}       else       {           answer = (a * 3) + 1;           count++;           cout<<answer<<endl;       }       } while(a!=1);```
• 12-18-2006
muran_pling
Quote:

Originally Posted by I BLcK I
use a do-while loop because you dont know how many iterations your going through, but with a for loop you do.

Code:

```do{       if (a%2==0)       {answer = a/2;}       else       {           answer = (a * 3) + 1;           count++;           cout<<answer<<endl;       }       } while(a!=1);```

can u do it without the DO while loop ?
• 12-18-2006
spoon!
a while loop is probably better, so that it works for a = 1

also, inside the loop, you probably want to assign the things back to "a", like "a = a/2" and "a = a * 3 + 1"

Code:

```int steps(int a) {     int count = 0;         while (a > 1)     {       if (a%2==0)       {       a = a/2;             }       else       {       a = (a * 3) + 1;       count++;       }     }         return count; }```
• 12-18-2006
muran_pling
Quote:

Originally Posted by spoon!
a while loop is probably better, so that it works for a = 1

also, inside the loop, you probably want to assign the things back to "a", like "a = a/2" and "a = a * 3 + 1"

Code:

```int steps(int a) {     int count = 0;         while (a > 1)     {       if (a%2==0)       {       a = a/2;             }       else       {       a = (a * 3) + 1;       count++;       }     }         return count; }```

thanks spoon, it was those small changes that made it work!!
appreciate it :)
• 12-18-2006
I BLcK I
Its much more simplier and logically reasonable to use a do while loop, but i dont see why you would want to use a for loop. To answer your question, yes I can do it with a do, and for loop, but its much more comprehendable to use a do-while loop if someone else where to read your code.
• 12-18-2006
muran_pling
Quote:

Originally Posted by I BLcK I
Its much more simplier and logically reasonable to use a do while loop, but i dont see why you would want to use a for loop. To answer your question, yes I can do it with a do, and for loop, but its much more comprehendable to use a do-while loop if someone else where to read your code.

ok thanks mate! much appreciated :)