Write a program that generates some hailstones. Your program should use the func
ID: 3543099 • Letter: W
Question
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.
Use only #include <stdio.h> and #include<stdlib.h>
***All the other codes listed on Chegg only prompt me to input a number and then it does not print back the hailstones generated, nor the number of total hailstones generated.***
HELP!!
Explanation / Answer
please rate- thanks
any questions ask
#include <stdio.h>
#include<stdlib.h>
void hailstones(long);
int main()
{
long i;
printf("enter starting number: ");
scanf("%ld",&i);
hailstones(i);
return 0;
}
void hailstones(long n)
{int count=1;
printf("Hailstones generated by %d: %d ",n,n);
while(n!=1)
{if(n%2==0)
n/=2;
else
n=n*3+1;
count++;
printf("%d ",n);
}
printf(" ");
printf("hailstones gererated: %d ",count);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.