MATLAB Write a function called roman that takes a string representing an integer
ID: 3767204 • Letter: M
Question
MATLAB
Write a function called roman that takes a string representing an integer between 1 and 20 inclusive using Roman numerals and returns the Arabic equivalent as a number of type uint8. If the input is illegal or its value is larger than 20, roman returns 0 instead. The rules for Roman numerals can be found here: http://en.wikipedia.org/wiki/Roman_numerals. Use the definition at the beginning of the page under the “Reading Roman Numerals” heading. In order to have unambiguous one-to-one mapping from roman to Arabic numbers, consider only the shortest possible roman representation as legal. Therefore, only three consecutive symbols can be the same (IIII or VIIII are illegal, but IV and IX are fine). Also, a subtractive notation cannot be followed by an additive one using the same symbols making strange combinations, such as IXI for 10 or IXX for 19, illegal also.
Explanation / Answer
function arabicnumeral=roman(x)
switch x
case 'I'
arabicnumeral=1;
case 'II'
arabicnumeral=2;
case 'III'
arabicnumeral=3;
case 'IV'
arabicnumeral=4;
case 'V'
arabicnumeral=5;
case 'VI'
arabicnumeral=6;
case 'VII'
arabicnumeral=7;
case 'VIII'
arabicnumeral=8;
case 'IX'
arabicnumeral=9;
case 'X'
arabicnumeral=10;
case 'XI'
arabicnumeral=11;
case 'XII'
arabicnumeral=12;
case 'XIII'
arabicnumeral=13;
case 'XIV'
arabicnumeral=14;
case 'XV'
arabicnumeral=15;
case 'XVI'
arabicnumeral=16;
case 'XVII'
arabicnumeral=17;
case 'XVIII'
arabicnumeral=18;
case 'XIX'
arabicnumeral=19;
case 'XX'
arabicnumeral=20;
otherwise
arabicnumeral=0;
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.