Hey guys,
So I'm pretty proud of myself that I was able to get this program running properly (for the most part). However, the part I was least worried about (the sum) seems to be spitting out odd numbers and I have moved the sum+=count all over the place trying to get the correct output. As I understand it, my counter keeps running and I can tally the numbers to my sum total so it lets me know how many "hailstones" there are. My guess is that instead of adding like 1+1+1+1...etc. it's adding like 1+2+3+4...etc. Regardless of placement, I seem to get an incorrect sum.
I suppose that I am searching for a proper way to get this sucker to display how many hailstones there are. Hailstones in this sense are nothing more than how many times numbers show up until the number goes back to one. (The old math equation where you can take any number and if it is even, divide by 2, if it is odd multiply by 3, add 1 and it will always come back down to 1.) So for example, the number 15 should have 18 hailstones and the number 9 should have 20 hailstones. Sorry for the lengthy explanation.
Here it is so far
Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num;
int count;
int sum =0;
cout<<"Enter in a positive number to hailstone down to one!!-->";
cin>>num;
while (num<1)
{
cout<<"Stick to positive integers above 0 and re-enter a number-->";
cin>>num;
}
cout<<"The Hailstone sequence for "<<num<<" is:"<<endl;
cout<<setw(4)<<num;
for (count=1;count<=7;count++)
{
sum=sum+count;
if ((num%2==0) && (num>1))
{
(num = num/2);
cout<<fixed<<showpoint<<setprecision(4);
cout<<setw(4)<<num;
}
else if ((num%2!=0) && (num>1))
{
(num = ((num*3)+1));
cout<<fixed<<showpoint<<setprecision(4);
cout<<setw(4)<<num;
}
}
cout<<endl;
while (num>1)
{
for (count=1;count<=8;count++)
{ sum=(count+sum);
if ((num%2==0) && (num>1))
{
(num = num/2);
cout<<fixed<<showpoint<<setprecision(4);
cout<<setw(4)<<num;
}
else if ((num%2!=0) && (num>1))
{
(num = ((num*3)+1));
cout<<fixed<<showpoint<<setprecision(4);
cout<<setw(4)<<num;
}
}
cout<<endl;
}
cout<<"Your number has "<<sum<<" Hailstones."<<endl;
system("PAUSE");
return 0;
}
Thanks guys,
Techgique