Question

In: Computer Science

Write a function that tells whether a hailstone sequence contains a number that is greater than...

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.


C++

Solutions

Expert Solution

#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


Related Solutions

16. Write a function that returns the start value of a hailstone sequence that contains the...
16. Write a function that returns the start value of a hailstone sequence that contains the largest value that was reported by largestInAnyHS(n). Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns the start value from 1 to n of the hailstone sequence that contains the largest value. The heading must be int startHSWithLargest(int n) This function must not read or write anything. Modify your main function so that it...
Write if logic in Python that checks if a variable named number is either greater than,...
Write if logic in Python that checks if a variable named number is either greater than, less than , or equal to 10 and prints out the number with the appropriate message, for example, if number is 6 you would output: number,"less than 10" or 6 less than 10
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
Write a function, hexDigits that when passed an int array of any length greater than 0...
Write a function, hexDigits that when passed an int array of any length greater than 0 will print the corresponding hex digit for each int, one per line. The corresponding hex digit should be determined using a switch statement. The hex digits must be printed in uppercase and in order starting with the first entry. Each line of output should start with the array index of the number being written out, followed by a space, then the number, then another...
write a program using the main method where the user enters a number greater than 0,...
write a program using the main method where the user enters a number greater than 0, and the program prints out a set of stairs. The stairs consist of 3 boxes stacked on top of each other where the length and width of the first box is the number, the length and width of the second box is the number plus 1, and the length and width of the third box is the number plus 2. The stairs should be...
A prime number (or prime) is a natural number greater than 1 that has no posítive...
A prime number (or prime) is a natural number greater than 1 that has no posítive divisors other than 1 and itself. Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Given the language L={w| the number of a’s is greater than or equal to the number...
Given the language L={w| the number of a’s is greater than or equal to the number of b’s in w} a) Using the Pumping Lemma to prove L is not a regular language. b) Using closure property to prove L is not a regular language.
For each, indicate whether the first item is (greater than, equal to, less than) the second...
For each, indicate whether the first item is (greater than, equal to, less than) the second item oxygen content in the pulmonary veins      ___________       oxygen content in the carotid arteries Answer 1Choose...greater thanequal toless than maximum pressure in the aorta ___________     maximum pressure in the left atrium   Answer 2Choose...greater thanequal toless than blood flow through the lungs ____________ blood flow through the kidneys Answer 3Choose...greater thanequal toless than flow of blood through a dilated vessel _____________ flow of blood through...
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT