Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use MATLAB PROBLEM #2 Mrite a script/function that converts a Roman numeral to i

ID: 3597436 • Letter: U

Question

Use MATLAB

PROBLEM #2 Mrite a script/function that converts a Roman numeral to its decimal equivalent. There are two distinct situations that you might design your program for: The "old" style where the order of the symbols does not matter. In this case, IX and XI both mean 10+ 1 or 11 You should be able to handle the following conversion table: Roman Decimal 10 50 100 500 1000 The "new" style where the order of the symbols does matter For example, IX is 9 (10-1), XC is 90 (100- 10) The conversion table given above still holds and you may assume for this case that the only instances of "order" you will encounter are: IV (4), IX (9), XL (40), XC (90), CD (400) and CM (900) It would be a go d idea t try case 1 first. Use Menu to prompt user to select old or New style Ask user to enter the Roman numeral as a string >> Rn = input ( . Roman numeral : ' ,' s ' ) Then, call a user defined function to print the equivalent decimal value

Explanation / Answer

function [x]=roman2decimal(s)

s1=substring(s,1,1);

s2=substring(s,2,2);

s=substring(s,3,numel(s));

%case 1: 2 roman numbers;

numbers1=[4 9 40 90 400 900];

letters1=['I' 'I' 'X' 'X' 'C' 'C'];

letters2=['V' 'X' 'L' 'C' 'D' 'M'];

%if case 1 fails, we have case 2

numbers3=[ 1 5 10 50 100 500 1000];

letters3=['I' 'V' 'X' 'L' 'C' 'D' 'M'];

x=0;

s1=substring(s,1,1), s2=substring(s,2,2), s=substring(s,3,numel(s));

while (s~='')

case1=false;

case2=false;

i = 1;

while(i<=numel(numbers1) &~case1)

if (strcmp(s1,letters1(i)) & strcmp(s2,letters2(i)))

case1=true

x = x+numbers1(i) %update x

end

end

if(case1) %update s1, s2, s by advancing 2 characters to the right

s1=substring(s,1,1), s2=substring(s,2,2), s=substring(s,3,length(s))

else % case2

while(i<= numel(numbers3) & ~case2)

if (strcmp(s1,letters3(i))) %case2 applies % 1 letter match

case2=true

x = x+numbers3(i)%update x

end % if

end

end % if(case1)

if(case2), %update s1, s2, s by advancing 1 character to the right

s1=s2;

s2=substring(s,1,1),

s=substring(s,2,length(s));

if(~case1 && ~case2) error('Incorrect roman numeral')

end

end

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote