Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

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 button
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 Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education