It's been a few years since I wrote one of these. Try to do a few examples by hand. Say the nuumer 43... What do you do? Try a "L"? Too high? Try a bunch of "X"s? More than three? Etc. Eventually you'll arrive at a method.

03-28-2011

luigiramez12

The only thing I can find is how to do it in C++ when I search for it in google!

03-28-2011

nonoob

Well I'm sure your teacher doesn't want you to simply find the program. As a programmer, you are expected to devise the solution on your own. Using plus, minus, times, divide, decisions, etc.

03-28-2011

GReaper

Quote:

Originally Posted by luigiramez12

The only thing I can find is how to do it in C++ when I search for it in google!

Roman numerals have strict rules about how they're printed ( e.g. you can write "VIII" or "IX" or "XVI" but can you write "IXV"? )
Maybe you should search a little about that too.

03-28-2011

Adak

Quote:

Originally Posted by luigiramez12

The only thing I can find is how to do it in C++ when I search for it in google!

Programming trick # 46:

There is an association in many problems involving numbers or letters, that can be exploited, because the numbers and letters are consecutive.

They are consecutive.

You can associate an int array with a char array, using their index number, in different arrays.

(number is an int array, word is a 2d char array: word[][]
then you can make:

number[0] = word[0] = "zero"
number[1] = word[1] = "one"
etc.

Your problem is different, but this trick can be used to solve it, and many others.

Your fears are fully justified - take a fresh start.

03-29-2011

iMalc

Learn about 'lookup tables', or at the very least 'switch statements'. You should use that instead of a whole lot of it-then-else cases.
Using a lookup table for each decimal digit in the input number is a reasonable way to go.

03-30-2011

nonoob

A lookup table for representing Roman digit groups for decimal '0' to '9', depending on their positioning as units, tens, hundreds would be one way.

Or make a loop to replicate 'I's, even after a "V"... I, II, III vs. VI, VII, VIII. Special exception for IV, IX. Then use lookup / indexing into a string depending on whether 'I' and 'V' should be scaled to 'X' and 'L' respectively. That way you're not duplicating a lot of code.

It’s a fun little program actually.

03-30-2011

WJG

Ok, it ain't C but, this would be plagiarism and tut-tut! Here's a solution using Tcl. It might give some insight.

A lookup table for representing Roman digit groups for decimal '0' to '9', depending on their positioning as units, tens, hundreds would be one way.

Or make a loop to replicate 'I's, even after a "V"... I, II, III vs. VI, VII, VIII. Special exception for IV, IX. Then use lookup / indexing into a string depending on whether 'I' and 'V' should be scaled to 'X' and 'L' respectively. That way you're not duplicating a lot of code.

Itís a fun little program actually.

The big problem with roman numerals is that they are not positional math.
Beyond that there are certain combinations where you subtract rather than add.

Not that complicated. Although there are wrinkles I was not aware of...

Such as you can't use 'I' as a subtractor to anything but its immediate next decade. "IX" is OK, but "IC" is not for 99, nor are you allowed "IM" for 999. And you are not allowed to use any of the fiver units "V", "L", "D" as subtractors. Otherwise 95 could be written as "VC" and 1995 could be "MVM".