By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Integers to Roman Numerals Question

P: 3
I've written the program and it works fine. but my instructor said that i can make the program short and use functions in it, but i cant really understand functions that good. so i just need a little help making the program short and use functions too. I'm posting my code here but i'm not posting the cin statements and the validating user entry loops. i'm just posting the if statements that i used. so here is my code:

thousands = number / 100; // number is the user input.
hundreds = number / 100 % 10;
tens = number / 10 % 10;
ones = number % 10;

(thousands == 1 ? roman += "M":
thousands == 2 ? roman += "MM":
thousands == 3 ? roman += "MMM":
hundreds == 1 ? roman += "C":
hundreds == 2 ? roman += "CC":
hundreds == 3 ? roman += "CCC":
hundreds == 4 ? roman += "CD":
hundreds == 5 ? roman += "D":
hundreds == 6 ? roman += "DC":
hundreds == 7 ? roman += "DCC":
hundreds == 8 ? roman += "DCCC":
hundreds == 9 ? roman += "CM"
: roman = roman);

(tens == 1 ? roman += "X":
tens == 2 ? roman += "XX":
tens == 3 ? roman += "XXX":
tens == 4 ? roman += "XL":
tens == 5 ? roman += "L":
tens == 6 ? roman += "LX":
tens == 7 ? roman += "LXX":
tens == 8 ? roman += "LXXX":
tens == 9 ? roman += "XC"
: roman = roman);

(ones == 1 ? roman += "I":
ones == 2 ? roman += "II":
ones == 3 ? roman += "III":
ones == 4 ? roman += "IV":
ones == 5 ? roman += "V":
ones == 6 ? roman += "VI":
ones == 7 ? roman += "VII":
ones == 8 ? roman += "VIII":
ones == 9 ? roman += "IX"
: roman = roman);
Mar 22 '07 #1
Share this Question
Share on Google+
2 Replies


dmjpro
100+
P: 2,476
try to write a function and then report the problem u facing ....
Mar 23 '07 #2

DeMan
100+
P: 1,806
The key to creating these functions (and in fact any function) is to work out the rules that have to be followed....So first consider the rules that Roman Numerals follow.....
We can think of a common pattern with several exceptions, but thankfully even these exceptions follow patterns.....

Your code will probably work reasonably well, but I suspect the instructor is looking for something that follows rules, rather than explicitly setting the values.

In the simplest case (for you) you could write a function that does exactly what you do now and returns a string with the entire value. It sounds like the instructor would award more marks, however, If you created seperate functions for units, tens, hundreds etc (which you could still call from one function called from main if you liked).

I suppose the biggest idea in an exercise such as this, though, is to simplify a process. Your solution, while providing the user with an answer, hasn't simplified the proccess.

Hope these thoughts help....
Post again if you get stuck and need further help!

Just some ideas, hope they helped....
Mar 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.