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

Can anyone help with this MATLAB problem??? Thank you so much!! Write a function

ID: 3785942 • Letter: C

Question

Can anyone help with this MATLAB problem??? Thank you so much!!

Write a function called rom2num that converts Roman numerals to numbers. Recall characters in Roman numerals are

numeral

number

I

1

V

5

X

10

L

50

C

100

D

500

M

1000

The input is a valid Roman numeral entered as a string. The output is a number. For example, rom2num(‘VII’) yields 7.

There are many approaches. See section 9.2 of van Loan for an approach. We will discuss section 9.2 in lecture 5. A simpler approach is the following.

Write a function called Converter that takes the strings ‘I’,‘V’,‘X’,‘L’,‘C’,‘D’,‘M’ and returns the corresponding numbers. Use the switch case construction for the different possibilities.

Set S=0.

Read the characters from left to right

At each step call the function Converter. Add the corresponding value to S.

At each step check the value of the previous character. If it is less, then subtract two times that value from S.

numeral

number

I

1

V

5

X

10

L

50

C

100

D

500

M

1000

Explanation / Answer

MATLAB CODE

function s=rom2num(x) % function to convert rom to number
   s = 0; OldVal =0;% variables
   for k = 1:length(x) % loop to take individual characters
       NewVal = convert(x(k));% Converting to number
       s =s + NewVal; % adding to the result
       if(OldVal< NewVal)% Checking previous number is lesser or not
           s = s - 2*OldVal;% If less then substract 2 times
       end
       OldVal = NewVal;
   end
end
function v = convert(x)% Function to convert each letters to number
      switch x % using switch
          case 'I'
              v = 1;
          case 'V'
              v = 5;
          case 'X'
              v = 10;
          case 'L'
              v = 50;
          case 'C'
              v = 100;
          case 'D'
              v = 500;
          case 'M'
              v = 1000;
      end
end

OUTPUT

>> rom2num('VII')

ans =

     7

>> rom2num('VIII')

ans =

     8

>> rom2num('IV')

ans =

     4

>> rom2num('IX')

ans =

     9

>> rom2num('X')

ans =

    10

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