One Function at a Time (Hailstone Sequence) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

One Function at a Time (Hailstone Sequence)

Good afternoon everyone, I'm currently working on the following program: __________________________________________________________ // 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 l); int[] hailstone_List [ ]; //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: " << hailstone_Sequence(user_Input) << endl; 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) { if (n > 1) { while (n != 1) { if ((n % 2) == 0 ) { return n / 2; } else { return (3*n) + 1; } } } } // int lengthof_Sequence(int l); // { // // // // // // } // void Print_Set(int [] set, int size) // { // // // // } ____________________________________________________ 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: #, #, #, #, #,

24th Jan 2017, 9:11 PM
Progress101
Progress101 - avatar
1 Answer
0
***Just noticed it cut off the rest of my question*** 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. _______________________________ I'll look into taking out the returns from the loop in hailstone_Sequence for starters. Something like this: /* 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 length //Array function here; } return n; } }
24th Jan 2017, 9:16 PM
Progress101
Progress101 - avatar