In: Computer Science
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.
#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;
}