counting steps

This is a discussion on counting steps within the C++ Programming forums, part of the General Programming Boards category; ok heres my problem.. 1. if x is even, divide x by 2. 2. if x is odd, multiply x ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    33

    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;
    }
    thanks in advanced!!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    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);

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    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 ?

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    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;
    }

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    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

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    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.
    Last edited by I BLcK I; 12-18-2006 at 04:00 AM.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! -Linked Lists, Memory Allocation, Time Steps, Debugg
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-14-2009, 09:50 PM
  2. Counting up(and down) using recursive function. Please help.
    By MarkSquall in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2008, 05:26 AM
  3. How to implement reference counting with 'Smart_ptr'?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2007, 06:28 AM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21