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

Image Transcription

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?

See Answer

Check out a sample Q&A here.

Want to see this answer and more?

Step-by-step answers are written by subject experts who are available 24/7. Questions are typically answered in as fast as 30 minutes.*

See Answer
*Response times vary by subject and question complexity. Median response time is 34 minutes and may be longer for new subjects.

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A

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...