Thread: The Wonderful Hailstones!

  1. #1
    Registered User Techgique's Avatar
    Join Date
    Jun 2011
    Location
    San Diego
    Posts
    9

    Wink The Wonderful Hailstones!

    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

  2. #2
    Registered User Techgique's Avatar
    Join Date
    Jun 2011
    Location
    San Diego
    Posts
    9

    Red face

    Almost as quickly as I sat there staring at the post, it dawned on me if anyone was interested. I simply placed a sum++ inside the if/else statements and then added one to the final amount to account for the initial number.

    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++)
              {
                  
                        if ((num%2==0) && (num>1))
                            {
                            (num = num/2);
                            cout<<fixed<<showpoint<<setprecision(4);
                            cout<<setw(4)<<num;
                            sum++;
                            }
                            else if ((num%2!=0) && (num>1))
                            {
                            (num = ((num*3)+1));  
                            cout<<fixed<<showpoint<<setprecision(4);
                            cout<<setw(4)<<num;
                            sum++;
                            }    
                              
              }
              cout<<endl;
              
              
             
        while (num>1)
        {
              for (count=1;count<=8;count++)
              {   
                        if ((num%2==0) && (num>1))
                            {
                            (num = num/2);
                            cout<<fixed<<showpoint<<setprecision(4);
                            cout<<setw(4)<<num;
                            sum++;
                            }
                            else if ((num%2!=0) && (num>1))
                            {
                            (num = ((num*3)+1));  
                            cout<<fixed<<showpoint<<setprecision(4);
                            cout<<setw(4)<<num;
                            sum++;
                            }   
                         
                               
              }
              
              cout<<endl;
              
        }     
        
        cout<<"Your number has "<<(sum+1)<<" Hailstones."<<endl;
        
        system("PAUSE");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Oh wonderful binary, where art thou?
    By cboard_member in forum C++ Programming
    Replies: 10
    Last Post: 07-15-2005, 12:00 PM
  2. Congrats on a wonderful resource site
    By boredman_007 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-28-2004, 07:12 PM