Question

My assignment wants me to program the hailstone series. I have done that, but my program isn't executing because I did not put it in the format of returning a string, instead I cout the n value. How can I make it so it returns the series as a string?

Hailstone
Editor
1 #include <string>
Problem Statement
Take a number, n. If n is even, divide it by 2. If odd, multiply by 3 and add
1. Repeat with the new number, forever or until you get the number 1. For
instance, suppose you start with the number 3:
3 string sequence(int n) f
4 cout <<n <<"";
5- whileCn-1)
6
7
if(n%2-0)(
Since 3 is odd, you multiply by 3 and add 1 to get 9+1-10
Since 10 is even, you divide by 2 to get 5
Since 5 is odd, you multiply by 3 and add 1 to get 16
Since 16 is a power of 2, you will repeatedly divide by 2, getting 8, 4,
2, and 1 in that order
cout << n<<""
}else if(n%2-1)(
10
cout << n<<""
The sequence of numbers produced by this procedure is known as a
hailstone sequence. The hailstone sequence beginning with 3 is therefore
"3 10 5 16 8 4 21". The Collatz conjecture, proposed by Lothar Collatz in
1937, is that the hailstone sequence for any starting number always
terminates; that is, it always reaches 1. This conjecture is unproven at this
time, but is easy to verify for small n
12
13
14
15
Your task for this lab is to produce the hailstone sequence for an arbitrary
starting value. You are given an int value n, and asked to return a
string containing the hailstone sequence beginning at n. The string
should contain each number in the sequence in order, separated by single
spaces. There should be no leading or trailing spaces
Constraints
. The input n will be between 1 and 100, inclusive
Test
Submit
Expand
Transcribed Image Text

Hailstone Editor 1 #include <string> Problem Statement Take a number, n. If n is even, divide it by 2. If odd, multiply by 3 and add 1. Repeat with the new number, forever or until you get the number 1. For instance, suppose you start with the number 3: 3 string sequence(int n) f 4 cout <<n <<""; 5- whileCn-1) 6 7 if(n%2-0)( Since 3 is odd, you multiply by 3 and add 1 to get 9+1-10 Since 10 is even, you divide by 2 to get 5 Since 5 is odd, you multiply by 3 and add 1 to get 16 Since 16 is a power of 2, you will repeatedly divide by 2, getting 8, 4, 2, and 1 in that order cout << n<<"" }else if(n%2-1)( 10 cout << n<<"" The sequence of numbers produced by this procedure is known as a hailstone sequence. The hailstone sequence beginning with 3 is therefore "3 10 5 16 8 4 21". The Collatz conjecture, proposed by Lothar Collatz in 1937, is that the hailstone sequence for any starting number always terminates; that is, it always reaches 1. This conjecture is unproven at this time, but is easy to verify for small n 12 13 14 15 Your task for this lab is to produce the hailstone sequence for an arbitrary starting value. You are given an int value n, and asked to return a string containing the hailstone sequence beginning at n. The string should contain each number in the sequence in order, separated by single spaces. There should be no leading or trailing spaces Constraints . The input n will be between 1 and 100, inclusive Test Submit

Expert Answer

Want to see the step-by-step answer?

Check out a sample Q&A here.

Want to see this answer and more?

Experts are waiting 24/7 to provide step-by-step solutions in as fast as 30 minutes!*

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Tagged in
Engineering
Computer Science

Related Computer Science Q&A

Find answers to questions asked by students like you.

Q: Show a derivation tree for the string aabbbb with the grammar S→AB|λ,A→aB,B→Sb.

A: Given:Grammar: S → AB | λ, A → aB, B → SbString: aabbbbLet us take a top-down approach,Starts with t...

Q: 1.6 Compute the scalar cardinality and the fuzzy cardinality for each of thefollowing fuzzy set.

A: The scalar cardinality of a fuzzy set is calculated using the formula:

Q: Find context-free grammars for the following languages (with n ≥ 0, m ≥ 0)  L = {anbm : n = m − 1}.

A: Solution:A context-free grammar (CFG) is a hard and fast of recursive rewriting guidelines which is ...

Q: When disposing disks or hard drives, it is recommended to format them (writing binary zeros) at leas...

A: Reason of formatting the disks or hard drives is as follows:

Q: 5-1) (Count positive and negative numbers and compute the average of numbers) Write a program that r...

A: Java program to count posive and negative numbers and computing the total and average of numbers is ...

Q: SQL (Oracle) How do I create a unique constraint to find a specific price range? i.e. between 30 - 1...

A: Query to create tabale:Let us assume the table name as "Product". CREATE TABLE PRODUCT(ORDER_ID INT,...

Q: write a program in c++. You work for a box company, and have been asked to write a program to compar...

A: Click to see the answer

Q: 4-20) (Process a string) Write a program that prompts the user to enter a string and displays its le...

A: Program to enter the string by the user to display its length and first character.

Q: What are the internal components of a computer that make it function?

A: A computer system consists of multiple hardware devices which, with the help of software, perform co...

Q: How to write a function in python where it takes a list of string s and finds the index of the longe...

A: The program has the following algorithm:Intitialize the counter counting string list position number...

Q: 1) The UsingMethod class consists of a main method and a method called xMethod. What will the code p...

A: Hey, since there are multiple questions posted, we will answer first question. If you want any speci...

Q: How to write a function in python where it takes a list of string s. Where s= ["hello","lazyness","p...

A: The program has the following algorithm:Intitialize the counter counting string list position number...

Q: Assume that you have been tasked by your employer to develop an incident response plan. Create a lis...

A: Stakeholder:A stakeholder can be a person, a group, or an organization that has an affair or matter ...

Q: Associate each of the following concepts with one of the languages we covered (pseudo code,FORTRAN, ...

A: Associating languages with given concepts:ALGOL 60: Pass by nameALGOL 60: Pass by valueFORTRAN: Assi...

Q: Write a complete C++ program that randomly generates 20 integers between the rangeof 1 to 50 and sto...

A: Solution:Given data,The C++ code to generate the 20 random numbers with the range from “1” to “50”, ...

Q: Construct the XOR operator using only AND, OR, and NOT gates.

A: XOR gate:An XOR gate is referred to an Exclusive OR gate. The output of an XOR gate is “1” only when...

Q: I need guidance on this, more so what direction do I take to append to a string and make sure it is ...

A: To append the character "D" and "A" into string you can use append(); function of string class.  As ...

Q: Part 1: Stack As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue libr...

A: Import the given class in another file.Create the function postfix() which takes a string as its arg...

Q: use java Develop a program which allows the user to enter numbers into an array. Input will be as fo...

A: Program plan:Import the required packages.Define a class named “ArraySort”.Define a “main()” method....

Q: Visual Basic. Given two variables, isEmpty of type boolean, indicating whether a class roster is emp...

A: We have to check two conditions:-1) class roster is not empty 2) The class has more than two credits...

Q: Describe, in English, the language defined by the following grammar in BNF (start symbol is ):

A: Solution:The given grammar is,&lt;S&gt; :: = &lt;A&gt; |&lt;B&gt;&lt;C&gt; :: = a | c&lt;A&gt; :: = ...

Q: Find an nfa that accepts the complement from L (ab∗aa + bba∗ab)?

A: Solution:The following graph indicates the NFA (Non-deterministic finite automata) is given below:M ...

Q: Give the derivation tree for ((a+b)∗c+d, using the grammar in Example 5.12. EXAMPLE 5.12 To rewrite ...

A: To solve your problem first of all we define  What is Context Free grammer  and  Derivation tree? Co...

Q: Write a grammar for the language consisting of strings built only of theletters aand b. The strings ...

A: The given language L contains the strings that have infinite number of letters but the strings must ...

Q: What type of data breach occurred involving the Facebook/Cambridge Analytica?

A: Data breaching involves intentional or unintentional leakage of confidential information to un autho...

Q: Find all strings in L ((ab + b)∗ b (a + ab)∗) of length less than four.

A: Answer:The symbol “+”denoted as “OR” operator which means “a + b” means either “a” or “b”, so the la...

Q: Write a program in Eclipse that prompts the user for first name, last name, then secret number. The ...

A: Program plan:Include the required header file.Define the class “Main”.Define the “defaultInfo” metho...

Q: getRandomLib.h: // This library provides a few helpful functions to return random values// * getRand...

A: Program Instructions:Include the necessary header files.Print hello message and as for a response.De...

Q: How to write a function in python 3 that takes two dictionaries b1 and b2 and returns a new dictiona...

A: Solution:Given data,The python function takes two dictionaries “b1” and “b2” and return the combinat...

Q: Consider the following grammar in BNF (start symbol is ) Which of the following sentences are in the...

A: Backus-Naur Form (BNF):BNF is a formal mathematical method to define the syntax of the programming l...

Q: Consider the languages below. For each, make a conjecture whether or not it is regular. Then prove y...

A: The given language L = {anbl : n/l is an integer} is not regular language because a DFA cannot be co...

Q: Load “Lock5Data” into your R console. Load “OlympicMarathon” data set in“Lock5Data”. This data set c...

A: First we install the Lock5Data package and then load the package. Then we load the library dplyr to ...

Q: What values will print? Use Eclipse to verify it but only after you write down youranswers based on ...

A: Description of the Java Math utilities used:-Math.sqrt() returns the square root of its paramter a d...

Q: how to write a function in python 3 using for loop that counts how many times a consonant (either lo...

A: Note: The given program is compiled and executed in "repl.it" online compiler.

Q: CHALLENGE 1.24: Read multiple user inputs. ACTIVITY Write two scnr.nextint statements to get input v...

A: Given below is the program to get input values for month and year and write the output value as a mo...

Q: How do I define a recursive function with C++? I am supposed to compute the Fibonacci numbers with a...

A: The algorithm for the recursive Fibonacci series is as below:Start with the first element which is 0...

Q: Is each of the following an absolute pathname, a relative pathname or a simple file name?a. Milk_cob...

A: Absolute path specifies the location of a directory or file from the root directory including a slas...

Q: How do I get this flat list table into 1NF, 2NF and 3NF?

A: Normalization:Normalization is a process of dividing the tables by more than one idea into set of ta...

Q: Weighted Intervals Problem EXAMPLE:Suppose we have the following weighted intervals. start   finish ...

A: Instructions for solving the problem.Sort the given intervals by finish time, and the intervals with...

Q: I am trying to convert a number to hexadecimal. How do I do this without using modulus, division, an...

A: To convert a decimal number into hexadecimal form with out using any of modulus, division or multipl...

Q: Write a PL/SQL block to retrieve and display the students enrolled in a course.

A: Click to see the answer

Q: 1. What is the output of the following code? public class Loopy {public static void main(String[] ar...

A: 1. Algorithm for the given code is as follows:The variable ""x is initialized as "1" within the "mai...

Q: Attached Files:  getRandomLib.h: // This library provides a few helpful functions to return rand...

A: Program Instructions:Include the necessary header files.Print hello message and as for a response.De...

Q: what are some of the challenges that can face a firm to combine “a clear brand vision with a common ...

A: Introduction:The combination of clear brand vision of an organization with a common operating model ...

Q: Consider a 32-bit hexadecimal number stored in memory as follows: Address Value 100 2A 101...

A: Answer:In Little Endian the order is reversed. So the binary representation of reversed order is as ...

Q: Assume that an array of integers named a that contains exactly five elements has been declared and i...

A: Didn’t mention which language to do the problem. So, did the problem in Java.Given:Let assume an arr...

Q: Hello, I have a question with the program javascript and in the program I need to create a timer in ...

A: To display the greeting message, we can declare a new variable, suppose wish, to store the greeting ...

Q: What would the output be when the user taps or clicks the btnDrivingAge button   Private Sub btnDri...

A: Form1:

Q: You are not required to execute the queries. However, your syntax must by accurate.You must give at ...

A: Since the full table structure is not given we make suitable assumptions to write the query:For ques...

Q: How would I write a C program to find the length of the longest substring of a given string without ...

A: The given program is to find the maximum length of the substring without repeating character. This i...