1

I need to write a function that calculates and returns the length of the hailstone sequence previously calculated in the functions that are already there. Everything I've tried gives me an infinite loop of "22"s. Arrays are NOT allowed. Everything has to be done with loops and only one loop per function.

I have mostly tried using the previous functions with length++; added to them. But I am just clueless as to what to do.

#include <cstdio>
using namespace std;

// The function next(n)takes an integer value n and 
// returns the number that follows n in a hailstone sequence. 
// For example: next(7) = 22 and next(22) = 11.

int next(int n)
{
  if (n > 1)
  {            
    if ((n % 2) == 0 )
    {
      n = n / 2;
    }
      else
      {
        n = 3 * n + 1;
      }
    printf("%i ",n); 

  }          
  return 0;
}

// The function hailstone reads int n and 
// prints its entire hailstone sequence.

void hailstone(int n)
{
  while(n>1)
  {
    next(n);
  }
} 

int length(int n)
{
  int length = 1;
  return length;
}

int main()
{
  int n; 

  printf("What number shall I start with?");
  scanf("%i", &n);

  printf("The hailstone sequence starting at %i is: ", n);
  hailstone(n);


  printf("The length of the sequence is: %i", length(n));

  return 0;
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • Please, remark the best matching answer: [SO: What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) If there is no actual answer, you may leave comments why they does not match your expectation. Or, even better, [edit] your question and add details. – Scheff's Cat Feb 08 '18 at 07:03

3 Answers3

1

The problem is that you do not change the n value. Try this:

int next(int n)
{
  if (n > 1)
  {            
    // As before
  }          
  return n;
}

Note the return n; to return the next value in the sequence. Next we need:

void hailstone(int n)
{
  while(n>1)
  {
    n = next(n);
  }
} 

I changed this to n = next(n);, so we pick up the new value in the sequence.

Furthermore, the length could be calculated by:

int hailstone(int n)
{
  int length = 0;
  while(n>1)
  {
    n = next(n);
    length++;
  }
  return length;
} 

This counts how many times we call next().

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
1

First, in the loop of hailstone, you does not modify n because the parameter of next function is passed by value. You can change the parameter of next to a reference instead, or take advantage of the return value, i.e.

int next(int n)
{
    // ...       
    return n;
        // ^
}

and use it in the loop to modify n:

void hailstone(int n)
{
    while (n > 1)
    {
        n = next(n);
     // ^^^
    }
} 

Second, to compute the length, you should record it during the loop and, again, take advantage of the return value to pass the length information to the caller.

unsigned hailstone(int n) // return type is changed to unsigned
{
    unsigned length = 0;
    while (n > 1)
    {
        n = next(n);
        ++length;
    }
    return length;
} 

Then you can print the length information in main:

printf("The length of the sequence is: %u", hailstone(n));
xskxzr
  • 12,442
  • 12
  • 37
  • 77
0

The function next() takes an integer as an argument, and that value gets copied to a completely separate variable when the function is called. This means that any changes to the variable in the function has no effect at all on the variable whose value is used to call the function.

You should either use a reference like this: void next(int &n) (you will learn about references later in your journey into C++), use a global variable, or, given the context, I think it is best to just return the new value of n each time the call to next() ends. Just replace the line return 0; in next() with return n; (not sure why you had a function returning an integer and then you just returned 0), and then use this line to call the function: n = next(n);.

For the length, you can have a counter variable in the function hailstone(), increment it in the loop, and then just return the length.

The new code:

#include <cstdio>
using namespace std;

// The function next(n)takes an integer value n and 
// returns the number that follows n in a hailstone sequence. 
// For example: next(7) = 22 and next(22) = 11.

int next(int n)
{
  if (n > 1)
  {            
    if ((n % 2) == 0 )
    {
      n = n / 2;
    }
    else
    {
      n = 3 * n + 1;
    }
    printf("%i ",n);

  }          
  return n; //return the new value of n
}

// The function hailstone reads int n and 
// prints its entire hailstone sequence.

int hailstone(int n)
{
  int length = 0; //counter variable
  while(n>1)
  {
    n = next(n); //update n with the new value
    length++; //increment counter
  }
  return length; //return length to be used later
} 

int main()
{
  int n; 

  printf("What number shall I start with?");
  scanf("%i", &n);

  int length; //variable to store the length
  printf("The hailstone sequence starting at %i is: ", n);
  length = hailstone(n); //save the length from the function call

  printf("The length of the sequence is: %i", length);

  return 0;
}
eesiraed
  • 4,626
  • 4
  • 16
  • 34