Comp Sci Converting decimal to Roman Literal using programming?

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Programming
AI Thread Summary
The discussion focuses on converting decimal numbers to Roman numerals using JavaScript, highlighting issues with the initial code that fails to handle specific cases like 4 and 9 correctly. Participants emphasize the need for a more refined algorithm that accounts for special numeral combinations, suggesting a complete rewrite rather than minor adjustments. The importance of understanding the structure of Roman numerals, including their limitations, is also stressed. Debugging techniques, such as hand-simulation and using a debugger, are recommended to identify logical errors in the code. A comprehensive approach is necessary to ensure accurate conversion for all potential inputs.
shivajikobardan
Messages
637
Reaction score
54
Homework Statement
Converting decimal to Roman Literal using programming?
Relevant Equations
None
JavaScript:
var dict = {
  1: "I",
  5: "V",
  10: "X",
  50: "L",
  100: "C",
  500: "D",
  1000: "M",
};

ordinary_literal = Number(prompt("Enter the value of year in foramt 2010"));
//2010 as input
let roman_value = "";
let minDifference = Infinity;

while (ordinary_literal > 0) {
  for (var key in dict) {
    var difference = Math.abs(ordinary_literal - key);
    if (difference < minDifference) {
      minDifference = difference;
      key_to_use = key;
    }
  }

  roman_value += dict[key_to_use];
  ordinary_literal -= key_to_use;
}

console.log(roman_value);

The code fails when pressed 9.
It's because of dictionary. But how do I realize what's missing in dictionary? how do I realize what else to add in this dictionary? Is there a technique to do so?
 
Physics news on Phys.org
So you want it to print out IX for 9 but it printed out VIIII instead?
 
jedishrfu said:
So you want it to print out IX for 9 but it printed out VIIII instead?
it printed X for 9.
 
I don't have a specific algorithm in mind, but it appears that you have a lot less logic in your code than is needed. Roman numerals are a bit tricky, as certain numbers need to be handled as special cases, particularly 4's and 9's that can appear in various places in the decimal representation.
4 --> IV
9 --> IX
and similar for 14, 19, 24 29, etc.

40 --> XL
90 --> XC
and similar for 140, 190, 240, 290, etc.

400 --> CD
900 --> CM
and similar for 1400, 1900, 2400, 2900, etc.

Your difficulty with 9 showing up as X is a completely different situation. A good way to find the problem is to hand-simulate your code; that is, sit down with paper and pencil and analyze what your program is doing when you enter 9 as the input. An alternative is to use the Javascript debugger to single-step through your code to inspect the variable values at each step.
 
shivajikobardan said:
The code fails when pressed 9.
I'm guessing that's the first number you tried. I suspect that if you enter 2010 you'll get incorrect results as well.

Your algorithm needs to be refined, and by that I mean, completely rewritten. It's reasonable to assume that the largest input number is 3999, as there isn't a Roman numeral for 4000, as far as I'm aware -- they didn't usually put four Roman numerals of the same kind together, so you won't see IIII where they would use IV, or MMMM for 4000. Also there's not a symbol I know of for 5000.

The way I would do the problem is to figure out how many thousands there are ( < 4), how many hundreds, how many tens, how many ones, with special cases for 4 or 9 digits.
 
Look at your algorithm where you do abs.

10 was closer to 9 than 5 was right? But 10 is greater than 9 so what should you have done.

As @Mark44 has said in an earlier post Roman numerals are a bit more complex than your algorithm allows and you'll need to rethink it.
 
JavaScript:
var dict = {
  1: "I",
  4: "IV",
  5: "V",
  9: "IX",
  10: "X",
  40: "XL",
  50: "L",
  90: "XC",
  100: "C",
  500: "D",
  900: "CM",
  1000: "M",
};
ordinary_literal = Number(prompt("Enter the value of year in foramt 2010"));
//2010 as input
let roman_value = "";
let minDifference = Infinity;
while (ordinary_literal > 0) {
  for (var key in dict) {
    var difference = Math.abs(ordinary_literal - key);
    if (difference < minDifference) {
      minDifference = difference;
      key_to_use = key;
    }
  }
  roman_value += dict[key_to_use];
  ordinary_literal -= key_to_use;
}
console.log(roman_value);
Does this work? I've added couple of dictionary values. no, it doesn't work.
 
Last edited:
shivajikobardan said:
Does this work? I've added couple of dictionary values. no, it doesn't work
All you did was to add a few key/value pairs in your dictionary, but you didn't change the rest of your code, as far as I can see with a quick scan. As I said before, you need to completely revise your code if you want this to work. See post #5 for my suggestion.
 
Mark44 said:
The way I would do the problem is to figure out how many thousands there are ( < 4), how many hundreds, how many tens, how many ones, with special cases for 4 or 9 digits.
Or look up each decimal digit in a 2-dimensional table / array similar to this:

Code:
     1   10   100  1000
1    I    X     C     M
2   II   XX    CC    MM
3  III  XXX   CCC   MMM
4   IV   XL    CD
5    V    L     D
6   VI   LX    DC
7  VII  LXX   DCC
8 VIII LXXX  DCCC
9   IX   XC    CM
 

Similar threads

Replies
10
Views
3K
Replies
19
Views
2K
Replies
2
Views
8K
Replies
4
Views
3K
Back
Top