Wondrous Numbers Function
Download
printWondrous.c
,
or copy it into your current directory on a CSE system by running
$ cp /web/cs1511/17s2/week07/files/printWondrous.c .
You have been given a main function and an incomplete
printWondrous
function to complete.
This function takes in an integer and must print out the Wondrous Sequence starting with that number, followed by a single new line character.
The Wondrous Sequence is generated by the simple rule:
- if the current term is even: the next term is half the current term.
- if the current term is odd: the next term is three times the current term, plus 1.
For example the sequence generated by starting with 3 is:
3 10 5 16 8 4 2 1
Your function is to print a single new line character ‘\n’ after printing the final 1, and then nothing else.
Apart from printing out the sequence, your function must return the number of terms in the printed sequence (which the main function will print out).
Some Examples
Enter a number: 17 17 52 26 13 40 20 10 5 16 8 4 2 1 The count is 13.
Enter a number: 1 1 The count is 1.
To run some simple automated tests:
$ 1511 autotest printWondrous
To run Styl-o-matic:
$ 1511 stylomatic printWondrous.c Looks good!
You’ll get advice if you need to make changes to your code.
Submit your work with the give command, like so:
$ give cs1511 wk07_printWondrous
Or, if you are working from home, upload the relevant file(s) to the wk07_printWondrous activity on Give Online.