One Function at a Time (Hailstone Sequence)

Good afternoon everyone,

I'm currently working on the following program:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Tab stops:  4

//The program will use a given integer from the user to compute a
//a particular sequence. Tab tops 4.
#include <iostream>
#include <cstdio>
using namespace std;


//Function Variable Declaration.
int hailstone_Sequence(int n), lengthof_Sequence(int x);

//int[] hailstone_List [x];

//void Print_Set(int [] set, int size);



int main(int argc, char** argv)
{
    int user_Input; cin >> user_Input;


    cout << "What number shall I start with? " << user_Input << endl;

    cout << "The hailstone of sequence starting at "  << user_Input << " is: " << endl;
    cout << Print_Set(int [] set, int ))

    cout << "The length of the sequence is ___. \n";

    cout << "The largest number in the sequence is: \n";

    cout << "The longest hailstone sequence with a number up to __ has length ___ ";

    cout << "The longest hailstone sequence with a number up to __ begins with ___ ";
    return 0;
}


    /* The hailstone sequence takes an integer (n) that is greater than 1 from the user. If
    // n is even, computes n/2. If n is odd, computes 3n+1. Both are done till integer 1
    // is reached.
    */
    int hailstone_Sequence (int n)
    {
        int x = 0;
        
        if (n > 1)
        {
            while (n != 1)
            {
                if ((n % 2) == 0 )
                {
                   n = n / 2;
                }
                else
                {
                    n = (3*n) + 1;
                }
                x++;//Will be used for the array's lenght
                //Array function here;
            }
            return n;
        }

    }


//    int lengthof_Sequence(int x);
//    {
        
//
//
//
//
//
//    }


//    void Print_Set(int [] set, int size)//Still working on function
//    {
        for(int index = 0; index < size; index++) {
			System.out.print(set[index]);
			//Print commas after every integer except the last one
			if(index < size - 1){
				System.out.print(", ");
			}
		}
//
//
//
//    }


The program is to use an integer provided by the user for a Hailstone Sequence, then to list the Sequence out. Example:

user integer: 22

list: #, #, #, #, #,

I have an idea on what I would like done, but not sure how to go about it in cpp. Only have little under a month in this language. I'm looking to take the values from the hailstone_Sequence to be in array for display. Any advice would be much appreciated. Looking to learn as much as I can.

v/r

slaugher101
Why don't you print out the sequence as you are computing it in hailstone_sequence? You should also return x from this function, not n.

I don't think it would be a good idea to store the sequence in an array: nobody has managed to prove that this series is guaranteed to terminate!
If I'm not looking to place the value of the sequence in an array, then I have no use for 'x'. I came up with 'x' for it to later be potentially used as the array maxsize. How would I be able to print out each value from the sequence on one line without doing an array?

v/r

slaugher101
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

// assumes that if n is odd 3*n+1 would be within the range of unsigned int
unsigned int next( unsigned int n ) { return n%2 == 0 ? n/2 : 3*n + 1 ; }

void print_sequence( unsigned int n )
{
    if( n == 0 ) return ;

    unsigned int largest = 1 ;
    unsigned int length = 0 ;

    while( n != 1 )
    {
        std::cout << n << ", " ;
        ++length ;
        if( n > largest ) largest = n ;
        n = next(n) ;
    }

    std::cout << "1\n"
              << "length: " << length+1 << '\n'
              << "largest value: " << largest << '\n' ;
}

http://coliru.stacked-crooked.com/a/812a362c48b2345a
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
#include <iostream>
#include <vector>

int main()
{
    std::cout << "Enter number: \n";
    int num{};
    std::cin >> num; // add usual input validations
    std::vector<int> hSequence {num};
    while(num != 1)
    {
        if(num % 2 == 0)
        {
           num /= 2;
        }
        else
        {
           num = 3*num + 1;
        }
         hSequence.push_back(num);
    };
    for (auto& elem : hSequence)
    {
        std::cout << elem << " ";
    }
}
Topic archived. No new replies allowed.