1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
laila [671]
3 years ago
12

The length of a hailstone sequence is the number of terms it contains. For example, the hailstone sequence in example 1 (5, 16,

8, 4, 2, 1) has a length of 6 and the hailstone sequence in example 2 (8, 4, 2, 1) has a length of 4. Write the method hailstoneLength(int n), which returns the length of the hailstone sequence that starts with n.
Computers and Technology
1 answer:
telo118 [61]3 years ago
8 0

Answer:

  1. #include <iostream>
  2. using namespace std;
  3. int hailstoneLength(int n){
  4.    int term = 1;
  5.    while(n != 1){
  6.        if(n % 2 == 0){
  7.            n = n / 2;
  8.            term++;
  9.        }
  10.        else{
  11.            n = n * 3 + 1;
  12.            term++;
  13.        }
  14.    }
  15.    return term;
  16. }
  17. int main()
  18. {
  19.    cout<<hailstoneLength(8);
  20.    return 0;
  21. }

Explanation:

Hailstone sequence is a list of number that is generated by following the rules as below:

  1. Given a starting number (e.g. 5)
  2. If the number is even, divide it by 2
  3. If the number is odd, multiply it by 3 and then add 1
  4. the starting number will either be repeatedly divided or multiplied until the final n reaches 1

So, we can create a function called hailstoneLength that takes one input number, n, as starting number (Line 4).

In the function initialize the variable term with value of 1 (Line 5).  Create a while loop and set the condition to enable the loop running until the n become 1 (Line 6). In the loop, keep checking the n value if it is currently even or odd using modulus operator (Line 7 - 14). If it is even, divide n by 2 and increment the term by one (Line 7 - 10). If it is odd, multiple it by 3 and add 1 (Line 11 - 14). At last, return term value as output (Line 16).

In the main program, test the function by passing 8 as argument. We shall get the output of 4 (Line 21).  

You might be interested in
How many times will the loop body execute:
Xelga [282]
I’d also say B, which is 2
6 0
3 years ago
Emma would like to track profits made each month. She should use a
garri49 [273]
Database is the answer
6 0
3 years ago
Read 2 more answers
If we develop a new policy for our environment that requires us to use complex and automatically generated passwords that are un
stepan [7]

Answer:

The answer is "Login complexity will be increased very exponentially".

Explanation:

In computer science, The password is also known as a series of characters, which allows you to use the validation process to validate any customer's privacy.  

  • It is normal usage of passwords that work together with a username to make it, it only accessible by the user, and to give the user accessibility to a computer, software or web page.
  • If the password is more complex so, it is hard to learn, and it also provides login complexity, that's why we must use a less complex password.
7 0
3 years ago
What is true concerning physical and logical topologies? Physical topologies are concerned with how a network transfers frames.
Sindrei [870]

Answer:

Physical means the actual wires.  Physical is concerned with how the wires are connected. Logical is concerned with how they transmit.

Explanation:

8 0
3 years ago
By default, the pfsense firewall allows unrestricted outbound access from the lan interface. true or false?
Sav [38]

The statement "By default, the pfSense firewall allows unrestricted outbound access from the lan interface" is true.

pfSense is free and open source firewall and router that can be installed on a physical computer or a virtual machine. Teh goal is to make the machine a dedicated firewall/router for a network.

 It features unified threat management, load balancing, multi WAN etc.


8 0
4 years ago
Read 2 more answers
Other questions:
  • ATM machines respond to request in__________​
    13·1 answer
  • OSHA requirements state that employees must have eye or face protection if their work exposes them to hazards such as:
    8·1 answer
  • The central device on a network that provides a common connection point for nodes on that network is called the
    8·1 answer
  • 1. The trucks hauling asphalt have a maximum capacity of 5 US tons (10,000 lbs) 2. The standard road lane is 12 feet wide. 3. As
    14·1 answer
  • Write a program that accepts a file name from the command line, then initializes an array with test data using that text file as
    6·1 answer
  • printArray is a method that accepts one argument, an arrayof int. The method prints the contents of the array; it does not retur
    5·1 answer
  • Which module is missing from most expert systems? a. Knowledge base subsystem b. Inference engine c. User interface subsystem d.
    5·1 answer
  • Five advantages of Internet​
    6·2 answers
  • Which one of these is the range for a Class C address?
    12·2 answers
  • On tool hackers use to get sensitive information from victims is/are:
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!