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
NeX [460]
3 years ago
9

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. /** Returns the length of a hailstone sequence that starts with n, * as described in part (a). * Precondition: n > 0 */ public static int hailstoneLength(int n)
Computers and Technology
1 answer:
11111nata11111 [884]3 years ago
7 0

Answer:

Following are the program to this question:

#include <iostream> //defining header file

using namespace std;

int hailstoneLength(int n) //defining method hailstoneLength

{

int t=1; //defining integer variable assign  

while(n!=1) //define a loop that checks value is not equal to 1

{

if(n%2==0) // check even number condition

{

n=n/2; // divide the value by 2 and store its remainder value in n

t++; //increment value of t by 1.

}

else

{

n=n*3+1; //calculate and hold value in n  

t++; //increment value of t variable by 1  

}

}

return t; //return value

}

int main() //defining main method

{

int n; //defining integer variable

cout<<"Enter any number: "; //print message

cin>> n; //input value

cout<<hailstoneLength(n); //call method and print its value

return 0;

}

Output:

Enter any number: 3

8

Explanation:

Program description can be given as follows:

  • In the given C++ language program an integer method "hailstoneLength", is declared, that accepts an integer variable "n" in its parameter.
  • Inside the method, an integer variable t is declared, that assign a value that is 1, in the next line, a while loop is declared, that uses if block to check even condition if number is even it divide by 2 and increment t variable value by 1.
  • If the number is odd it will multiply the value by 3 and add 1 and increment t by 1 then it will go to if block to check value again. when value of n is not equal to 1 it will return t variable value.
  • In the main method, an integer variable "n" is used that call the method and print its return value.
You might be interested in
What is the organizational management practices on planning during a pandemic like corona virus.
eduard

Answer:

wear a mask use hand sanitizer dont get in contact with anyone between 6 feet

Explanation:

3 0
3 years ago
You should check your battery ___________. Every week Never Every six months Every 30,00 miles
vova2212 [387]

The answer is Every six months

A battery acts as the brain of a car and is used to power almost everything. It can last up to five years. However, this lifespan will depend on how well the battery is maintained and the type of weather conditions you live in. Aim to check your car battery at least twice a year. Actually, it is best to check your battery regularly and not to wait for your mechanic to test your battery during routine servicing.

5 0
3 years ago
Read 2 more answers
A strategy for speeding up hard drive performance is _________.
Daniel [21]

Answer:disk caching

Explanation:

Disk caching speeds up hard drive performance

7 0
3 years ago
Read 2 more answers
Which of the following Wi-Fi chalking method refers to drawing symbols in public places to advertise open Wi-Fi networks?
dezoksy [38]

Answer War Chalking

Explanation: War chalking is the technique for presenting the WiFi network publicly. This signifies about the WiFi is present but not describes the manner of it , that is whether it is open connection or closed connection.

It can be used by the people in general as well as hackers . Hackers usually tend to attack and hack its security that is present.Hackers then use the WiFi network  is then used for their own work after being hacked.

3 0
3 years ago
Read 2 more answers
In order for network monitoring to work properly, you need a PC and a network card running in what mode?
Natasha_Volkova [10]

Answer:

Promiscuous

Explanation:

5 0
3 years ago
Other questions:
  • Which markup language would be considered the most current for web design? HTLM, HTML5, XHTLM, XHTML 6
    14·2 answers
  • Both the USB flash drive and the DVD are data storage devices, but they both have numerous differences. Both can contain insane
    6·1 answer
  • Motivation is best defined as
    9·2 answers
  • You are given two processors P1 and P2 that execute the same instruction set but have different architectures. The instructions
    9·1 answer
  • Does the brain play a role in smartphone addiction
    7·2 answers
  • A user contacted the help desk to report that the laser printer in his department is wrinkling the paper when printed. The user
    5·1 answer
  • Why can’t I message people? It doesn’t let me, please help.
    12·2 answers
  • Refund please, this has not helped at all.
    6·2 answers
  • Which block in this module represents the variable?
    8·1 answer
  • When using the Internet, do not give out your __________ without your parents' permission. (Select all that apply.)
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!