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 loop to read 10 integers from the user. Each integer...
Write a program that uses a loop to read 10 integers from the user. Each integer will be in the range from -100 to 100. After all 10 integers have been read, output the largest and smallest values that were entered, each on its own line in that order. Avoiding using the max or min functions from Python, and definitely use a loop to read the integers instead of 10 input statements.
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
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 while loop that will let the user enter a series of integer values and...
Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user backwards. do Loop...
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT