Roman Numeral Counter

This is a discussion on Roman Numeral Counter within the C Programming forums, part of the General Programming Boards category; Hello, I am in a C programming class, and my instructor wants us to write a program that will count ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Roman Numeral Counter

    Hello,

    I am in a C programming class, and my instructor wants us to write a program that will count from 1 to 100, and print out the roman numeral equivalent to each number.

    The exact wording of the question:

    Write a program that prints a table of all the Roman numeral eqivalents of the decimal numbers in the range of 1 to 100

    I don't even know where to start.
    the chart should look like

    Number Roman Numeral
    ----------------------------------------------------------------
    1 I
    2 II


    I can get the chart to print by hard coding

    Code:
    int main (void)
    {
    
    	 int rom;
    	 
    
    		printf ("Decimal    Roman Numeral\n");
    		printf ("-----------------------------\n");
    
    	for (rom = 0; rom <= 100; rom= rom +1)
    	{
    		
    
    		if (rom >= 1 && rom < 4)
    		{
    		if (rom == 1)
    		printf ("%d                I\n\n", rom);
    
    		else 
    		if (rom == 2)
    		printf ("%d               II\n\n", rom);
    
    		else 
    		if (rom == 3)
    		printf ("%d               III\n\n", rom);
    
    		}//end if statment 
    
    		else 
    		if (rom >=4 && rom < 5)
    		printf ("%d               IV\n\n", rom);
    
    		else
    		if (rom >= 5 && rom <= 9)
    		{
    		
    		if (rom == 5)
    		printf ("%d                V\n\n", rom);
    
    		else 
    		if (rom == 6)
    		printf ("%d               VI\n\n", rom);
    
    		else 
    		if (rom == 7)
    		printf ("%d              VII\n\n", rom);
    
    		else 
    		if (rom == 8)
    		printf ("%d              VIII\n\n", rom);
    
    		else 
    		if (rom == 9)
    		printf ("%d              IX\n\n", rom);
    
    		}//end if statment 
    
    		
    	}// end for
    	
    
    
    
    
    
    	printf ("\n\n");
    	
    	return 0;
    }


    I am just afraid that this is too much hard coding for her. Is there an easier way to achieve that same results?

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Quote Originally Posted by luigiramez12 View Post
    Is there an easier way to achieve that same results?
    Yes. Since it's a C programming class, your instructor probably wants you to *program* the table, not hard code it.

    You're being asked to figure out an algorithm for converting from decimal to roman numerals.

    If you have no idea where to start on such an algorithm, there are examples on the web if you search.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Probably. The task is to write a program... for generating Roman Numerals.
    Your first attempt looks more like a table from 1 to 100 with all the results hard-coded.
    I = 1
    V = 5
    X = 10
    L = 50
    C = 100
    D = 500 (you won't need for numbers 1 - 100)
    M = 1000 (you won't need for numbers 1 - 100)

    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.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    The only thing I can find is how to do it in C++ when I search for it in google!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Quote Originally Posted by luigiramez12 View Post
    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.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by luigiramez12 View Post
    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.
    Last edited by Adak; 03-28-2011 at 02:40 PM.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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.

  10. #10
    WJG
    WJG is offline
    Registered User
    Join Date
    Mar 2010
    Location
    GB
    Posts
    1
    Ok, it ain't C but, this would be plagiarism and tut-tut! Here's a solution using Tcl. It might give some insight.

    Code:
    proc roman:numeral {i} {
            set res ""
            foreach {value roman} {
            1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 9 IX 5 V 4 IV 1 I} {
                    while {$i>=$value} {
                            append res $roman
                            incr i -$value
                    }
            }
            set res
     }

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    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.
    Code:
    I        = 1
    II       = 2
    III      = 3
    IV       = 4      <--- 5 - 1
    V        = 5      <--- back to single digit
    The rules are complex as all get out...

    Roman numerals - Wikipedia, the free encyclopedia

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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".

    WJG, the Tcl algorithm is devilishly clever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Roman Numeral troubles....
    By h4rrison.james in forum C Programming
    Replies: 16
    Last Post: 01-15-2009, 08:26 PM
  2. roman numeral conversion
    By clarky101 in forum C Programming
    Replies: 8
    Last Post: 12-09-2007, 06:13 PM
  3. Roman Numeral...T_T
    By only1st in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 10:06 AM
  4. Roman Numeral Calculator
    By The Brain in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2005, 06:23 PM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21