0

I made here a program to do 3x+1 math problem. So I am asking, if I could write in a c++ code something like x/2;, or x*3+1. These stuff what i put here are with mistakes. Then, is it possible in c++ to do that? If yes, how?

Here's the code:

#include <iostream>
using namespace std;
int main() {
    cout << "Write an integer.\n"; int x; cin >> x;
    // I made here a program to do 3x+1 math problem.
    while (x==1)  {
        if ((x%2)==0)  {
            x/2; cout << x << ", ";
        }  else if ((x%2)==1)  {
            x*3+1; cout << x << ", ";
        }
    }
    return 0;
}

The output there was:

/tmp/GjudkYOaE4.o
Write an integer.
9

But I was waiting it to write the number 28, 14, and more, but it did nothing.

mch
  • 9,424
  • 2
  • 28
  • 42
  • Because you will only enter in while loop when your `x` is equal to `1` – RC0993 Aug 27 '21 at 08:13
  • 8
    I suggest [getting a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You need to learn the basics before tackling problems. – Yksisarvinen Aug 27 '21 at 08:13
  • You said ` I was waiting ... 28, 14, and more,` untill when? what is the terminating condition? – RC0993 Aug 27 '21 at 08:17
  • Well, the loop that contains all of your logic runs `while (x==1)`. Is this condition satisfied when you input 9? or 28? or 14? – Lukas-T Aug 27 '21 at 08:18
  • This math problem is called the _Collatz conjecture_. – nada Aug 27 '21 at 08:21
  • 3
    I think you're looking for the `+=`, `-=`, `*=`, `/=`, `%=` operators (and a bunch of others). But no, there's no operator that would directly mean `x=3*x+1`. You'll have to spell it out as I did. – Ruslan Aug 27 '21 at 08:26
  • `x/2` doesn't mutate `x`, but "return" the computation (which you don't use, so discarded), you probably want `x = x / 2;` – Jarod42 Aug 27 '21 at 08:42

2 Answers2

1

I can see that you are new to coding, I would suggest you to please read a good amount of information about if-elseif-else and while loop that you are using

I can show you a few corrections here

#include <iostream>
using namespace std;
int main() {
    cout << "Write an integer.\n"; 
    int x; cin >> x;
    // I made here a program to do 3x+1 math problem.
    while (x!=1)  {                   // x!=1 could be one of the terminating condition so when your x becomes 1, the loop would end. But of course the terminating condition depends on what you are trying to achieve
        if (x % 2 == 0)  {
            x = x/2;                   //you need to assign the value back to x
            cout << x << ", ";
        }  else {                      // you dont need else-if here because there could only be 2 possibilities for %2 operaion here
            x = x*3+1; 
            cout << x << ", ";
        }
    }
    return 0;
}

RC0993
  • 898
  • 1
  • 13
  • 44
  • The 3x+1 problem, known as Collatz conjecture, indeed has `x==1` as the terminating condition. – Ruslan Aug 27 '21 at 08:29
  • yes I agree, so in while loop you would need to write `x!=1` to keep the loop going. and as it becomes `x==1` the loop terminates – RC0993 Aug 27 '21 at 08:30
  • Yeah, but the comment is inconsistent with this. – Ruslan Aug 27 '21 at 08:31
  • @RC0993 theoretically it can be an infinite loop since int size is implementation-defined and it is possible to have a loop in Collatz (or prove me wrong ;) ). I mean it is checked for every number which is lower than 2^68, but there is still a possibility. – Bandi Aug 27 '21 at 11:45
0

Give value to the same variable with assignment operator ('='):

x = x*3+1;

or

x = x/2;
Bandi
  • 66
  • 4