[help] 3n+1 problem

This is a discussion on [help] 3n+1 problem within the C++ Programming forums, part of the General Programming Boards category; The loop should begin like this: Code: unsigned i, j; while(cin >> i >> j) { int unsigned k,l,max_cycle=0; When ...

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The loop should begin like this:

    Code:
        unsigned i, j;
        while(cin >> i >> j)
        {
        int unsigned k,l,max_cycle=0;
    When entering values from the keyboard (rather than redirecting input to a file), you can indicate end of "file" (input) with a special key combination, e.g on Windows Ctrl+Z.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    K12
    K12 is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    Changed the loop but still got it wrong. Thanks anon.

    Regarding the iostream input buffer, I changed the code to include good(). But the code does not compile, can someone please tell me how to properly use this function.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int CycleLength(int x);
    
    int main()
    {
        int unsigned i,j;
        bool good();
        //i,j are user inputs of the two numbers. k,l store initial values of i and j respectively to be printed out later.
    
        while(good())
        {
        cin>>i>>j;
    
        int unsigned k,l,max_cycle=0;
    
        k=i;
        l=j;
    
        if(i==j)
            max_cycle=CycleLength(i);
        else
        if(i>j)
            {
                for(i;i>j-1;i--)
                {
                    if (CycleLength(i)>max_cycle)
                    max_cycle=CycleLength(i);
                }
            }
    
        for(i;i<j+1;i++)
        {
            if (CycleLength(i)>max_cycle)
                max_cycle=CycleLength(i);
        }
    
        cout<<k<<" "<<l<<" "<<max_cycle<<endl;
        }
    }
    
    int CycleLength(int x)
    {
            int count=1;
    
            while(x>1)
            {
                if (x%2==!0)
                        x=3*x+1;
                else
                        x=x/2;
            count++;
            }
            return count;
    }

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    By good() it was probably meant the member function cin.good().

    However, that would be a wrong way to control an input loop. You'll try to process input even if it fails, because you test for good only at the beginning of the next iteration.

    The correct way to control input loops is by testing the result of the input statement, as I've shown.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

Tags for this Thread


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