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