Sunday, August 2, 2009

Help me with an algorithm that takes a number and converts it into roman numeral?

hey guys i need help on how to make a program that reads an input number between 1000~3000 and gives you its roman numeral in the console. I have been really having trouble figuring out an algorithm for this and I need to do it for a class. If any of you have tips I'd greatly appreciate it.





(this is similar to what i have to make...


http://www.ivtech.com/roman/)


(also i use dev c++ 4)


(also ANY form of help is greatly appreciated)

Help me with an algorithm that takes a number and converts it into roman numeral?
Since you have a predefined range, you know the biggest roman numeral you will have to work with is M(1000), three times. I would start by determining the highest number roman numeral you will need.





Example: 2942


Does M(1000) fit?


- Yes


- How many times?


-Twice, print it twice


- subtract 1000 * number of times it fit from the number you are trying to make


- we now have 942


- Does C (500) fit?


- yes


- how many times?


- once, print C once





- No


- Does the next smaller one fit...I think that is C (500)








You can start to see a loop. Does my current roman numeral fit? Yes or No. If yes, how many times and subtract that from the number you are working with and print it that many times..., but there is a special case, when it would fit 4 times, then you need to print it once followed by the roman numeral one higher








if times_fit = 4


print("%c%c",curr_roman_numeral, prev_roman_numeral)


else


loop times_fit


print("%c", curr_roman_numeral)


end loop
Reply:try... www.1000projects.com


No comments:

Post a Comment