In: Computer Science
Write a function that tells whether a hailstone sequence contains a number that is greater than 1000.
Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns true if the hailstone sequence starting with n contains a number that is greater than 1000, and returns false otherwise. The heading must be as follows.
bool bigHailstone(int n)
This function must not read or write anything.
Your algorithm for bigHailstone must be a search algorithm. As soon as it sees a number that is greater than 1000, bigHailstone must return true without looking at any more numbers in the hailstone sequence starting with n.
Modify your main function so that it also shows whether there is a number that is greater than 1000.
#include <iostream>
using namespace std;
// determining if the sequence contains a number greater than 1000
bool bigHailstone(int n) {
if(n % 2 == 0) {
n = n / 2; // if n is even
if(n > 1000) {
return true;
}
if(n == 1) {
return false;
}
bigHailstone(n);
}
else{
n = (n * 3) + 1; // if n is odd
if(n > 1000) {
return true;
}
if(n == 1) {
return false;
}
bigHailstone(n);
}
// return false if sequence does not have any number greater than 1000
return false;
}
// main function
int main() {
// read number from user
int n;
cin >> n;
// call function and store result
bool result = bigHailstone(n);
// display the result
if(result == 1) {
cout << "True";
}
else{
cout << "False";
}
return 0;
}
FOR HELP PLEASE COMMENT.
THANK YOU