Question

In: Computer Science

Problem description Write a program that uses a loop to read integer values from the standard...

Problem description

Write a program that uses a loop to read integer values from the standard input stream. Each non-zero value, x, is the first number of a sequence.

Define a0 = x; an+1 = an / 2 if an is even; an+1 = 3 * an + 1 if an is odd. Then there exists an integer k such that ak = 1.

For each non-zero value of x, the program outputs the integer k such that ak = 1 and the numbers a0, a1, a2, ..., ak, the value of k, the largest number in the sequence, and its position in the sequence. (See Output specification.)

Input specification

The input will consist of a series of non-negative integers, one integer per line. All integers will be less than 65536. The last integer in the series is zero, signalling the end of input. You can assume that no operation overflows a 32-bit integer. (See Sample input.)

Output specification

The program writes to the standard output stream.

There will be two lines of output for each line of input. The output should be formatted exactly as specified.

For each non-zero integer input, you should output the sequence a0, a1, a2, ..., ak, terminated by a newline. On the next line you should output the value of k, the largest number in the sequence, and its position in the sequence. These three numbers should be separated by one space with all three numbers on one line, terminated by a newline. (See Sample interaction.)

Sample input

I have provided sample input and expected output files in our shared directory. For example:

$ cat /home/shared/cs135/kmess/pa05-input0.txt
24
106
7
0
$

Sample interaction

$ ./a.out < pa05-input0.txt
24 12 6 3 10 5 16 8 4 2 1
10 24 0
106 53 160 80 40 20 10 5 16 8 4 2 1
12 160 2
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
16 52 5
$

Note: If you use geany to build the executable, use pa05 instead of a.out.

Judge script

You can validate your program's output using the judge. I have provided a couple sets of input files and expected output files for you in our shared directory. Assuming you copied these files to your project directory, you can use the following command:

$ judge -p a.out -i pa05-input0.txt -o pa05-output0.txt

You may append the -v option to the above command to enable verbose output.

Note: If you use geany to build the executable, use pa05 instead of a.out.

Assignment-specific requirements

The General Programming Guidelines apply (e.g., readability, documentation, restricted keywords, etc). This program expects input from cin and output to cout; do not use any ifstream or ofstream variables in this program.

Solutions

Expert Solution

#include<iostream>
using namespace std;

/* Function name : calcSeries */
/* accepts: number for which the series is to be printed */
/* return none */
void calcSeries(int num)
{
        int countNum = 0;     /* countNum represents numbers in the series till 1 is encountered */
        int largestNum = -1;  /* largestNum will hold the largest number encountered in the series */
        int pos = -1;         /* pos holds the position of the largest number in the series */
        while (num != 1)      /* while number is not equal to 1, the series will be stopped when the generated number value is 1 */
        {
                cout << num << " ";   /* print number */
                if (num > largestNum)  /* if current number is greater than the largest number so far, replace largest number with number*/
                {
                        largestNum = num;
                        pos = countNum;
                }
                countNum++;         /* increment count of numbers, everytime a new number is generated */
                
                if (num % 2 == 0)  /* if number is  even then next number is num/2 otherwise 3 * num + 1 */
                {
                        num = num / 2;
                }
                else
                {
                        num = (3 * num) + 1;
                }
        }
        cout << num << endl;
        cout << countNum << " " << largestNum << " " << pos << endl;  /* print count of numbers, largest number and its position in series */
}
int main()
{
        int num;
        cin >> num;   /* get number from standard input */
        while (num != 0)  /* loop until the entered num is 0 */
        {
                calcSeries(num);  /* call series function */
                cin >> num;
        }
        return 0;
}


Related Solutions

Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output...
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output the largest of those numbers, you can assume any length for those numbers. 80x86 assembly language
write a for loop that uses the loop control variable to take on the values 0...
write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10. Execute the program by clicking the Run button at the bottom of the screen. Is the output the same?
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
Write a program that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Write a loop that reads positive integers from standard input, printing out those values that are...
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integerthat is not positive. The values should be separated by single blank spaces. Declare any variables that are needed. PLEASE ANSWER IN C
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT