C++ Programming Language: Enhance the code given by outputting: The largest number of the sequence a0 ,a1 ,a2 , ..., ak. The position of the largest number Test your program for the following values of x: 75, 111, 678, 732, 873, 2048, and 65535. Example: "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"

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.4: A Case Study: Rectangular To Polar Coordinate Conversion
Problem 9E: (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by...
icon
Related questions
Question

C++ Programming Language: Enhance the code given by outputting:

  1. The largest number of the sequence a0 ,a1 ,a2 , ..., ak.
  2. The position of the largest number

Test your program for the following values of x751116787328732048, and 65535.

Example:

"For example, for the input sequence: 75226113340170852561286432168421, the program output should contain the following:

The largest number of the sequence is 340 The position of the largest number is 4"

 

Code Given:

 
#include <iostream>
#include <iomanip>
 
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;

return0;
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 5 images

Blurred answer
Knowledge Booster
Problems on Dynamic Programming
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning