Error
Printable View
Error
Please use code tags when posting code, [code]your code here[/code]. Press EDIT and add them and some formatting.
Most of the code look alright but a few observations:
- You assign both the lower and the upper value to the same variable (num)
- In Hailstone you don't assign anything for an even number
- You need a loop in main that couts every returned value from the calls to Hailstone (similar to the code in lengthHail)
Looks OK. I have n idea what a lower/upper limit in a hailstone sequence is supposed to represent. All I know is that it has a starting sequence.Code:int Hailstone(int n)
{
if(n % 2 == 0)
{
n/2; <- This doesn't do anything Perhaps you want to assign the result to n?
}
else
{
n=3*n+1; //number is odd
}
return n; //returns the value
}
//Calculates the length of the hailstone sequence
int lengthHail(int num)
{
int count=1;
while(num != 1)
{
num = Hailstone(num);
count++; //adds one to count everytime a calculation is done
}
return count;
}
//Ask for user input and reads in two integers that represent the limit .
int main()
{
int num;;
cout << "Enter Lower Limit.\t";
cin >> num;
cout << "Enter Upper Limit.\t";
cin >> num;
cout << "Hailstone Sequence starting at " << num << " is " << Hailstone(num) << endl; //<- This doesn't print a sequence
Hailstone(num);
cout << "\nLength of sequence is" << lengthHail(num) << ".\n" << endl;
}
Your hailstone function works at calculating a single iteration. You use it correctly in lengthHail with a while loop. However, in you main, you are not.
If you want to store and see the entire sequence, you have to start either calling Hailstone in a loop, or recursivelly. Then, you can either print the result as you go, or store the result in a container.
So I was able to run the code but it seems that no matter what the length really is I keep getting the same length. So I'm guessing I need to do a for loop for upper as well?
Code:#include <iostream> //I/O Library C++
using namespace std; // defining reserved names
//Hailstone sequence takes any number as an input and divides it by 2 if the number is even otherwise multiply by 3 and add 1 if the number is odd.
//The idea is that no matter what the user inputs you will eventually come to the answer of 1 at the end of the sequence.
//Hailstone function takes the input of the current number then calculates the next number in the hailstone sequence
//Replaced f with Hailstone because couldn't get past error message otherwise
int Hailstone(int n)
{
if(n % 2 == 0)
{
n /= 2; //number is even
}
else
{
n = 3 * n + 1; //number is odd
}
return n; //returns the value
}
//Calculates the length of the hailstone sequence
int lengthHail(int num)
{
int count = 1;
while(num != 1)
{
num = Hailstone(num);
count++; //adds one to count everytime a calculation is done
}
return count;
}
//Ask for user input and reads in two integers that represent the limit .
int main()
{
int lower, upper;
int count = 0;
cout << "Enter Lower Limit.\t";
cin >> lower;
cout << "Enter Upper Limit.\t";
cin >> upper;
for(int i = lower;i <= upper; i++)
{
lower = i;
while(lower != 1)
{
cout << " " << Hailstone(lower) << " " << endl;
lower = Hailstone(lower);
count++;
}
cout << "\nLength of sequence is " << lengthHail(upper) << ".\n" << endl;
}
return 0;
}
I think I may have the off by one bug but I can't seem to figure out how to resolve the issue.
You get 8 because you don't really track the maximum in your program at all. You are just looking at the lenght of the last one of the checked sequences which is correctly 8. But the correct maximum for these limits isn't 18 either, it's 21 (length of the sequence starting with 19).
I checked this with a program that is the result of applying my own corrections to your original code. And I'm pretty sure it works... :)
I can't see how you get to that result with the code you posted. I get a maximum legth of 112 for these limits, which is the length of the sequence starting at 27, not the last one (28).Quote:
But then in another case, if I make the lower 9 and the upper 28 it produces both the correct sequence and length.
Yes, you're correct sorry must of typed the wrong number was studying for an exam. I understand that I don't really follow the upper but I already tried using a loop to follow it but when I tried running the program it kept printing out, "Length of Sequence is 8" so I'm not really sure how exactly to go about following upper correctly. If you could give me a hint, I'd greatly appreciate it.
You don't need an extra loop to track the maximum; you can just do that "on the fly" in the outermost loop (for) in main().
A short breakdown on how to do it: Declare a variable for the maximum before entering the loop, and initialize it to an impossibly low maximum value. 0 would be a natural choice for that. During each of the iterations, check whether the current sequence length is greater than the maximum you have so far, and if yes, adjust the maximum accordingly. Once you're done with the loop, you have your maximum.
The C++ code required to do this, BTW, would be much less characters than my description above. :)
HTH