ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
okay so I'm trying to write a program that will calculate the number of steps it takes to get to 1 in the 3x+1 problem. The rules for the 3x+1 problem are as follows:
If a number is even divide by 2
If a number is odd, triple it and add 1
Stop if you ever get to 1
I have a program written but it only gives me an answer of 4 everytime. I don't understand why though! Can anyone point out what my problem is???
Quote:
#include <cmath>
#include <iostream>
using namespace std;
//Function prototype.
int UlamSequenceLength(int& number);
int main()
{
int number;
long count;
long x;
cout << "Enter number: " << endl;
cin >> number;
while (number != 1)
{
UlamSequenceLength(number);
count++;
}
x = UlamSequenceLength(number);
cout << x << endl;
}
int UlamSequenceLength(int& number)
{
if(number%2 == 0)
number = number/2;
else
number = (number * 3) + 1;
return number;
}
is executed, number will always be 1. Thus, you'll always get the hailstones sequence 1, 4, 2, 1 when you call UlamSequenceLength a second time. Why are you doing that?
Please post code in code tags, not quote tags. code tags will preserve indentation, making your posted code a lot easier to read (assuming you have sane indentation in th original).
It looks like homework, so I won't try to answer the question directly. I would suggest stepping through the program with a debugger to understand what it is doing.
I'm so confused... if I assigned count to x...why would it matter which one I print? but I did remove the x and tried to just print count and it gave me some off the wall negative number. And Dan04..why am I doing what?
if I assigned count to x...why would it matter which one I print?
(In your first attempt) you never assigned count to x. You assigned to x the number in the sequence after 1 (which was invariably 4).
Quote:
Originally Posted by Dan04
You aren't initializing count.
Yes. Remember that local variables with automatic storage (those without the keyword static) are not initialized to anything (so the value they usually have is whatever data which was already on the stack).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.