Hailstone Sequence if else if loop

closed account (16Ciz8AR)
My assignment is to write a program that reads in a number from the user and then generates the hailstone sequence from that point
I'm not sure if I should use an if statement or string loop any advice?
The beginning of an attempt

#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> Numbers;
/// if n is equal to 1 the sequence has ended
/// if n is even divide the number by 2
/// if n is odd, multiply by 3 and add 1 to new number
int main()
{
n = num1
n%2 = num2
((3 ** n) + 1) = num3
vector<string>
num1 -- 8,4,2,1,

if(num1 == 4,2,1)
{
cout << "congradulations your hailstone sequence is has come to an end";
return 0;

}
else(num2 <= 8)
}
closed account (28poGNh0)
Welcome to this website please next time use code tags

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# include <iostream>
# include <vector>
using namespace std;

int main()
{
    vector<int>vect;

    int nbr;
    cout << "Enter your nbr -> ";cin >> nbr;

    while(nbr!=1)
    {
        if(nbr%2)//mean It's odd
        {
            nbr = nbr*3+1;
            vect.push_back(nbr);
        }else
        {
            nbr /= 2;
            vect.push_back(nbr);
        }
    }

    for(size_t i=0;i<vect.size();i++)
        cout << vect[i] << ",";
    cout << "\b ...";

    return 0;
}


enjoy
I would do it like this:

Have an integer, let's say x, and use cin or getline to assign a user number to that.
1
2
3
4
5
6
x = -1;
while(x < 0)
{
    cout << "Enter number:";
    cin >> x;
}


As for the hailstone, you just need this kinda thing: (pseudocode)
1
2
3
4
5
6
7
if(x == 1) 
end 
if(x % 2 == 0)
/ x by 2 
else
{
x = x * 3+ 1
Topic archived. No new replies allowed.