menu
bartleby
search
close search
Hit Return to see all results

HailstoneEditor1 #include Problem StatementTake a number, n. If n is even, divide it by 2. If odd, multiply by 3 and add1. Repeat with the new number, forever or until you get the number 1. Forinstance, suppose you start with the number 3:3 string sequence(int n) f4 cout <

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
help_outline

Image Transcriptionclose

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

fullscreen
check_circleAnswer
Step 1

To input integer value into a string you need to use stringstream class which is declared in sstream.h header file.  You need to declare a object of string class as out_string and stringstream class as ss under your function sequence() as shown below:

fullscreen
Step 2

Next, you need to use the left shift operator (<<) to provide the value of integer  n to object ss. After that usi...

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Our solutions are written by experts, many with advanced degrees, and available 24/7

See Solution
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

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

question_answer

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:

question_answer

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

question_answer

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:

question_answer

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

question_answer

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

question_answer

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

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

question_answer

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

Sorry about that. What wasn’t helpful?