Thread: Quick newbie question :)

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    82

    Quick newbie question :)

    Well my first post here, off to an embaressing start, but you can't get far with out asking some questions, and this one i hope is simple enough.

    Just started C++ and was having a go at writing this particular algorithm, after getting somewhere but really nowhere, i got the real deal someone else made and tried to jury rig some of his code into mine to get it to work (i know i know ).

    well here we go, the Good professional version

    Code:
     #include <iostream>
    using std::cout;
    using std::endl;
    using namespace std;
        
    int main(int argc, char* argv[])
    {
       int iCount = 1;
       int iNo = 22;
       int iTemp  = iNo;
    
    {
    
    
       while (iTemp != 1)
       {
          
          if (iTemp % 2 == 0)
          {
             iTemp /= 2;
          }
          
          else
          {
             iTemp = iTemp * 3 + 1;
          }
    
          ++iCount;
       }
       cout << "Cycle length of " << iNo << " is " << iCount << endl;
        cin.get();
      return 0;
    }  
    }
    and my version

    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main(int argc, char* argv[])
                             // Most important part of the program!
    {
                              // Need a variable...
      int count = 1;
      int N;
      int X = N;     
            cout<<"Input Number: ";
             cin>> N;
              cin.ignore();
     
     while (X != 1)
      {
      if (X % 2 == 0)
      {
        X /= 2;
    }  
     else
      {
         X = X * 3 + 1;
          } 
         ++count;   
    }      
     
    cout << " cycle length of " << X << " is " << count << endl; 
          cin.get();
          return 0;
    }
    where the good version gives the correct output, cycle length of 22 is 16.

    while mine gives for any number inputed, cycle length of 1 is 255

    i orginally didn't have int X = N; but considering the other had int itemp = ino; i added it anyway.

    well there you go feel free to laugh, i just was interested if anyone can figure out why it doesn't work .

    Regards Wolfe

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The original code outputs iNo, not iTemp, yet you output X instead of N?

    Also, you assign X to N, but you do that before N has any value, so X is just filled with garbage.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    thank you will try it now.

    edit: thanks nice to see where i went wrong, thought becuase i put X=N, that "cycle length of" X would output the original value for N that ws typed.

    Thanks again for the help
    Last edited by Cdrwolfe; 10-20-2005 at 05:15 PM.

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Well, for starters your X = N;…it is bad. You are setting X equal to N before N is initialized N to anything(I am surprised your compiler let you compile it).
    Just move it down a couple lines after N has had its value input by user.

    Then it will work just fine, but your output will be wrong because you output X as the original number instead of N. Just change the X to N in the last cout line you have.

    *edit*
    Bah! I was beat while typing!

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Thanks, works now.

    going to try and see if i can get it to perform it on numbers 1 through 10 then output each result on que.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  2. Newbie question
    By geomark in forum C Programming
    Replies: 11
    Last Post: 05-17-2004, 10:08 PM
  3. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  4. Quick question
    By Rapt in forum C Programming
    Replies: 5
    Last Post: 11-01-2003, 02:16 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM