Question: Enhance your program from Programming Exercise 13 by outputting: The largest number of the sequence a 0 ,a 1 ,a 2 , ..., a k
Enhance your program from Programming Exercise 13 by outputting:
The largest number of the sequence a0 ,a1 ,a2 , ..., ak.
The position of the largest number
For example, for the input sequence: 75, 226, 113, 340, 170, 85, 256, 128, 64, 32, 16, 8, 4, 2, 1, the program output should contain the following:
The largest number of the sequence is 340 The position of the largest number is 4
Test your program for the following values of x: 75, 111, 678, 732, 873, 2048, and 65535.
Code Given:
#include
#include
using namespace std;
int main()
{
long x;
int count;
long a_n;
cout << "Enter a nonnegative integer: ";
cin >> x;
cout << endl;
count = 0;
a_n = x;
cout << a_n << ", ";
while (a_n != 1)
{
if (a_n % 2 == 0)
a_n = a_n / 2;
else
a_n = 3 * a_n + 1;
count++;
cout << a_n <<", ";
}
cout << endl;
cout << "The integer k such that a_k = 1 is " << count << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
