While loop for Hailstone Sequence

Discussion in 'OT Technology' started by SPACECATAZ, Jan 24, 2009.

  1. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    I've run into a problem when trying to build a hailstone sequence.

    I'm at this point in my assignment:

    2)Include a function that takes an integer n and prints the hailstone sequence starting at n on one line, separating the numbers by spaces. This function should not print a newline character. Make the return type void, indicating that it does not yield an answer, it just does something.

    I understand that I must have some kind of loop to repeat the function calculating the next number in the sequence so it will display like below, but I'm at a loss about how to do that.

    What it should look like:

    What number shall I start with? 7

    The hailstone sequence starting at 7 is
    7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

    The length of the sequence is 17.


    What it looks like now:

    What number shall I start with? 7

    The hailstone sequence starting at 7 is 22.

    The length of the sequence is 17.

    :hsd:

    Code:
    #include <iostream>
    using namespace std;
    /*
    Calculates the next number in the hailstone sequence by
    taking the initial input from main() and checks to see if it is even or odd.
    Then after the check, it does a computation to calculate the next number.
    */
    int nextNo(int iNum)
    {
            if(iNum % 2 == 0)
            {
            iNum /= 2;
            }   else 
                    {
                        iNum = iNum * 3 + 1;
                    } 
    
        return iNum;
    }
    /*
    Calculates the lengeth of the hailstone sequence, by adding one to iCount 
    everytime a caculation is done by the while loop and iNum!=1.
    */
    int printSequence(int iNum)
    {
      int iCount = 1;
      while (iNum != 1)
      {
        iNum = nextNo(iNum);
        ++iCount;
      }
      return iCount;
    }
    int main()
    {
        int iNum;
        cout << "What number shall I start with?\t";
        cin >> iNum;
        cout << "The hailstone sequence starting at " << iNum << " is " 
        << nextNo(iNum) 
        << endl; 
        int sequenceLength = printSequence(iNum);
        cout << "The length of the sequence is " << sequenceLength << ".\n" << endl;
        
        system("PAUSE");
    }
    
    
     
    Last edited: Jan 24, 2009
  2. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    This is a hailstone sequence btw:

    Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You write a sequence of numbers, one after another. Start by writing n. If n is even, then the next number is n/2. If n is odd, then the next number is 3n + 1. Continue in this way until you write the number 1.

    For example, if you start at 7, then the next number is 22 (3 times 7 plus 1). The next number after 22 is 11. The hailstone sequence starting at 7 is (7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1), and it contains 17 numbers. The hailstone sequence starting at 6 is (6, 3, 10, 5, 16, 8, 4, 2, 1), and the hailstone sequence starting at 1 is (1).
     
  3. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    I added the bold text to my code:

    Code:
    #include <iostream>
    using namespace std;
    /*
    Calculates the next number in the hailstone sequence by
    taking the initial input from main() and checks to see if it is even or odd.
    Then after the check, it does a computation to calculate the next number.
    */
    int nextNo(int iNum)
    {
            if(iNum % 2 == 0)
            {
            iNum /= 2;
            }   else 
                    {
                        iNum = iNum * 3 + 1;
                    } 
        return iNum;
    }
    /*
    Calculates the lengeth of the hailstone sequence, by adding one to iCount 
    everytime a caculation is done by the while loop and iNum!=1.
    */
    int printSequence(int iNum)
    {
      int iCount = 1;
      while (iNum != 1)
      {
        iNum = nextNo(iNum);
        ++iCount;
      }
      return iCount;
    }
    
    [B] //Calculates the hailstone sequence
    void calculateSequence()
    {
      while (iNum != 1)
      {
        iNum = nextNo(iNum);
      }
    
    }[/B] 
    
    
    int main()
    {
        int iNum;
        cout << "What number shall I start with?\t";
        cin >> iNum;
        cout << "The hailstone sequence starting at " << iNum << " is " 
        [B]<< calculateSequence(nextNo()) [/B]
        << endl; 
        int sequenceLength = printSequence(iNum);
        cout << "The length of the sequence is " << sequenceLength << ".\n" << endl;
        
        system("PAUSE");
    }
    
    But the "Make the return type void, indicating that it does not yield an answer, it just does something" part of the assignment for displaying the sequence is throwing me for a fucking loop :hs:.
     
  4. CodeX

    CodeX Guest

    Code:
    int num = 7;
    
    While(num != 1)
    {
       if(num % 2)  //if odd
       {
          num = num * 3 + 1;
       }
       else //if even
       {
          num /= 2;
       }
    }
    
    if it arbitrarily needs to be a function just print the value at the end of each iteration of the loop within the function itself:

    Code:
    void PrintSequence(int num)
    {
        While(num != 1)
        {
           if(num % 2)  //if odd
          {
             num = num * 3 + 1;
          }
          else //if even
          {
             num /= 2;
          }
          
          cout << num << ", ";
       }
    }
    
    Or, alternatively if your professor insists on a void return type and you want to return a value pass it as a pointer by reference


    Code:
    
    somewhere in main:
    while(num != 1)
    {
       CalcNextInSequence(&num);
       cout << num << ", ";
    }
    
    
    void CalcNextInSequence(int* num)
    {
        if(*num % 2)  //if odd
       {
          *num = *num * 3 + 1;
       }
       else //if even
       {
          *num /= 2;
       }
    }
    
     
    Last edited by a moderator: Jan 24, 2009
  5. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    Thanks man, :bowdown:. I haven't used pointers before, but it can't be that hard.
     

Share This Page