Monday, March 26, 2012

HW 9: Numbers

For this homework you will write a program that prints out the numbers between 1 and 1000000 in English. That is, it will print out:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifthteen
sixteen
seventeen
eightteen
nineteen
twenty
twentyone
twentytwo
twentythree
twentyfour
twentyfive
twentysix

...and so on...

one hundred and ninetyseven
one hundred and ninetyeight
one hundred and ninetynine
two hundred
two hundred and one
two hundred and two
two hundred and three
two hundred and four
two hundred and five
two hundred and six
two hundred and seven

...and so on...

nine hundred and ninetynine thousand nine hundred and ninetyfive
nine hundred and ninetynine thousand nine hundred and ninetysix
nine hundred and ninetynine thousand nine hundred and ninetyseven
nine hundred and ninetynine thousand nine hundred and ninetyeight
nine hundred and ninetynine thousand nine hundred and ninetynine
one million

You can download all the numbers if you want to see all of them.

Note that you must use a recursive method to generate the numbers. Look carefully at the numbers and notice the pattern, for example, the English for 136 is "one", then the words "hundred and" then the English for 36.

This homework is due Monday, April 2 @noon in the dropbox.cse.sc.edu.

8 comments:

Anonymous said...

I just want to make sure, are we supposed to use the "Numbers" file you provided?

Jose Vidal said...

No. That's just in case you want to check, say, how 736,345 is written in English.

Anonymous said...

Works fine counting up to 1,000. When I try to go to 10,000 it starts output at 1952 and I get a stack overflow error after 2,975.

Jose Vidal said...

You should not need more than 5 levels of recursion.

Anonymous said...

So we only need 5 levels of recursion, but will we need massive amounts of if-then statements?

Jose Vidal said...

Around 5 levels, and the program is about one page long.

Anonymous said...

Does a stack overflow error mean the program is stuck in an infinite loop?

Jose Vidal said...

Probably. A stackoverflow happens when a method calls itself more than 1024 (or, some number like that, the specific number can be set as a command-line argument to the jvm) recursive times without returning.

I explain this in my recursion video: http://www.csce145.com/2012/03/recursion.html