From book: 7. Let n0 be a given positive integer. For i = 0, 1, 2, ... define if
ID: 3530205 • Letter: F
Question
From book: 7. Let n0 be a given positive integer. For i = 0, 1, 2, ... define if ni is even, then ni+1 = ni/2 if ni is odd, then ni+1 = 3ni + 1 if ni is 1, the sequence ends Numbers that are generated this way are called hailstones. Write a program that generates some hailstones. Your program should use the function void hailstones(int n); to compute and print the sequence generated by n. The output of your program might look as follows: Hailstones generated by 77: 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Number of hailstones generated : 23 You might find that all the sequences you generate are finite. Whether this is true in general is still an open question. Hint: Use variables of type long instead of int if the program misbehaves From assignment sheet: You have to implement a function with the following prototype: void hailstones(int n); Example can be found in the textbook. The user only input the first hailstone number. All the rest and the total number of hailstones should be output by your program.Explanation / Answer
global variable: count=0; void hailstones(int n) { count++; printf("%d", &n); if(n==1) return; if(n%2 == 0) hailstones(n/2); else hailstones(3*n + 1); } printf("Number of hailstones : %d",%count);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.