Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++Program You have learned that functions are a very powerful tool in programmi

ID: 3811867 • Letter: C

Question

C++Program

You have learned that functions are a very powerful tool in programming and would like to start using them in your programs. You plan to write a simple program LongestSequence.cpp which relies on functions for its functionality.
The program explores the properties of the 3n + 1 sequence which has two very simple rules for generating its elements: starting with a positive number, if the current number n is odd, the next one is 3n + 1; if it is even, the next one is n/2. The number 1 is a special case. If the current number is 1, the sequence terminates and there is no next element. A few examples below with different starting values:
- start = 3: 3, 10, 5, 16, 8, 4, 2, 1 has 8 elements - start = 4: 4, 2, 1 has 3 elements - start = 1: 1 has 1 element
It is conjectured, but never proven, that the 3n+1 sequence always reaches 1 and terminates, regardless of its starting value. We can't check infinitely many start values, but we know how to program and we can check quite a few. We plan to see for ourselves.
The program asks the user to enter the start and the end of the range for start values to check. We will generate a sequence for each of the values in the given range. When (or if? :) ) the program terminates, we will output the longest sequence we encountered and the number of elements it has.
A few sample runs are given below:
Sample Run 1
Enter the min of the range for the sequence to start 1 Enter the max of the range for the sequence to start 5 The longest sequence with a start value in the range [1, 5] has 8 elements. 3, 10, 5, 16, 8, 4, 2, 1 We did not find a non terminating case.
Sample Run 2
Enter the min of the range for the sequence to start 10 Enter the max of the range for the sequence to start 100 The longest sequence with a start value in the range [10, 100] has 119 elements. 97, 292, 146, 73, 220, 110, 55, 166, 83, 250, 125, 376, 188, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1 We did not find a non terminating case.
Sample Run 3
Enter the min of the range for the sequence to start 100 Enter the max of the range for the sequence to start 100 The longest sequence with a start value in the range [100, 100] has 26 elements. 100, 50, 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 We did not find a non terminating case.

Explanation / Answer

#include<iostream>
using namespace std;
//Main function
int main()
{
int min, max, count = 1, longest = 0, no, number;
//Accept the minimum and maximum number
cout<<"Enter the min of the range for the sequence to start: ";
cin>>min;
cout<<"Enter the max of the range for the sequence to start: " ;
cin>>max;
//loops while minimum is less than or equals to maximum
while(min <= max)
{
no = min;
//Loops till 1
while(no != 1)
{
//Checks for even
if(no % 2 == 0)
no = (int)no/2;
//Otherwise odd
else
no = (no * 3 ) + 1;
//Counter is increased by one
count++;
}//End of inner while
//Checks for the longest counter
if(count > longest)
{
//Update the longest with the new counter
longest = count;
//Update the number with minimum
number = min;
}//End of if
//Reinitialize the counter
count = 1;
//Increase the minimum by 1
min++;
}//End of outer loop
cout<<" The longest sequence with a start value in the range [1, 5] has "<<longest<<" elements. ";
//Displays the sequence of number having longest sequence
while(number != 1)
{
cout<<number<<" ";
if(number % 2 == 0)
number = (int)number / 2;
else
number = (number * 3 ) + 1;
}//End of while
cout<<number;
return 1;
}//End of main

Sample run1:

Enter the min of the range for the sequence to start: 10
Enter the max of the range for the sequence to start: 100

The longest sequence with a start value in the range [1, 5] has 119 elements. 97 292 146 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1

Sample run2:

Enter the min of the range for the sequence to start: 1
Enter the max of the range for the sequence to start: 5

The longest sequence with a start value in the range [1, 5] has 8 elements. 3 10 5 16 8 4 2 1

Sample run3:

Enter the min of the range for the sequence to start: 100
Enter the max of the range for the sequence to start: 100

The longest sequence with a start value in the range [1, 5] has 26 elements. 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote