The 3n+ 1 problem is based on a famous sequence in mathematics that follows a very simple rule: • If the number is even, the next number in the sequence is its half, • If the number is odd, the next number in the sequence is three times the number plus one. In mathematical notation, the 3n+ 1 problem sequence is given as: an 2 if an is even = an+1 {3an +1 if an is odd It has been conjectured that for any positive integer number, the sequence will always end in 4, 2, 1. So, if the sequence starts with 7, the sequence is 7 22 11 34 17 52 26 13 40 20 10 5 16 8 421 In this task, you are required to write a program that takes as input two positive inputs, one for the start of the sequence, followed by an index. Your code must: Generate the above sequence inside a 1D array. Your code must stop when it reaches the last number (i.e., 1). • Your code must print the sequence number specified by the index. If the input index is larger than the sequence length, then the code outputs NA • Your code must also find and print the highest number the sequence reaches. • You must check if the inputs are positive numbers; otherwise, convert them to a positive number. • You must ensure if the start point is not a zero or one; otherwise, print out "Invalid" IMPORTANT NOTE • Do not add any cout statements except for the final answers as specified above. • Do not add "Enter a number", "the number of digits is" or any similar prompts. • Also note that the automatic grader is case-sensitive; so "na" is wrong but "NA" is correct. 11 . Do not add any unnecessary spaces inside the strings of cout statements " unless we ask you to. • You may add any libraries needed.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

C++ 

Chapter 7 - 1D Array - The 3n + 1 problem

8:09 P
The 3n+ 1 problem is based on a famous
sequence in mathematics that follows a very
simple rule:
• If the number is even, the next number in
the sequence is its half,
• If the number is odd, the next number in
the sequence is three times the number
plus one.
In mathematical notation, the 3n + 1 problem
sequence is given as:
an
2
if an is even
an+1
{
3αn +1
if an is odd
It has been conjectured that for any positive
integer number, the sequence will always end in
4, 2, 1.
So, if the sequence starts with 7, the sequence is
7 22 11 34 17 52 26 13 40 20 10 5 168421
In this task, you are required to write a
program that takes as input two positive
inputs, one for the start of the sequence,
followed by an index. Your code must:
• Generate the above sequence inside a 1D
array. Your code must stop when it reaches
the last number (i.e., 1).
• Your code must print the sequence number
specified by the index. If the input index is
larger than the sequence length, then the
code outputs NA
• Your code must also find and print the
highest number the sequence reaches.
• You must check if the inputs are positive
numbers; otherwise, convert them to a
positive number.
• You must ensure if the start point is not a
zero or one; otherwise, print out "Invalid"
IMPORTANT NOTE
Do not add any cout statements
except for the final answers as
specified above.
• Do not add "Enter a number", "the
number of digits is" or any similar
prompts.
• Also note that the automatic grader is
case-sensitive; so "na" is wrong but
"NA" is correct.
• Do not add any unnecessary spaces
inside the strings of cout statements "
" unless we ask you to.
• You may add any libraries needed.
I/O
Program Input:
=
Transcribed Image Text:8:09 P The 3n+ 1 problem is based on a famous sequence in mathematics that follows a very simple rule: • If the number is even, the next number in the sequence is its half, • If the number is odd, the next number in the sequence is three times the number plus one. In mathematical notation, the 3n + 1 problem sequence is given as: an 2 if an is even an+1 { 3αn +1 if an is odd It has been conjectured that for any positive integer number, the sequence will always end in 4, 2, 1. So, if the sequence starts with 7, the sequence is 7 22 11 34 17 52 26 13 40 20 10 5 168421 In this task, you are required to write a program that takes as input two positive inputs, one for the start of the sequence, followed by an index. Your code must: • Generate the above sequence inside a 1D array. Your code must stop when it reaches the last number (i.e., 1). • Your code must print the sequence number specified by the index. If the input index is larger than the sequence length, then the code outputs NA • Your code must also find and print the highest number the sequence reaches. • You must check if the inputs are positive numbers; otherwise, convert them to a positive number. • You must ensure if the start point is not a zero or one; otherwise, print out "Invalid" IMPORTANT NOTE Do not add any cout statements except for the final answers as specified above. • Do not add "Enter a number", "the number of digits is" or any similar prompts. • Also note that the automatic grader is case-sensitive; so "na" is wrong but "NA" is correct. • Do not add any unnecessary spaces inside the strings of cout statements " " unless we ask you to. • You may add any libraries needed. I/O Program Input: =
8:09 P
I/O
Program Input:
• One number that indicates the starting
number of the sequence
• One number that specified the index
of the number to be printed from the
sequence
Program Output:
• One line that prints the sequence
number at the specified index
• One line that outputs the highest
number in the sequence.
Sample Testcase 0:
Input:
7
8
Output:
40
52
Sample Testcase 1:
Input:
7
19
Output:
ΝΑ
52
Sample Testcase 2:
Input:
-10
-5
Output:
2
16
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4
5 int main()
6 {
7
=
int
8
9
array[1000]
int start = 0;
int numAtIndex
cin>>start;
cin>>numAtIndex;
= 0;
10
11
12
13
// Your code start here
14
15
// Your code end here
16
17
return 0;
18
19 }
{};
Transcribed Image Text:8:09 P I/O Program Input: • One number that indicates the starting number of the sequence • One number that specified the index of the number to be printed from the sequence Program Output: • One line that prints the sequence number at the specified index • One line that outputs the highest number in the sequence. Sample Testcase 0: Input: 7 8 Output: 40 52 Sample Testcase 1: Input: 7 19 Output: ΝΑ 52 Sample Testcase 2: Input: -10 -5 Output: 2 16 1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 int main() 6 { 7 = int 8 9 array[1000] int start = 0; int numAtIndex cin>>start; cin>>numAtIndex; = 0; 10 11 12 13 // Your code start here 14 15 // Your code end here 16 17 return 0; 18 19 } {};
Expert Solution
steps

Step by step

Solved in 4 steps with 7 images

Blurred answer
Knowledge Booster
Computational Systems
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.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education