Design a program to convert a Roman numeral to a decimal number. The program sho
ID: 3808182 • Letter: D
Question
Design a program to convert a Roman numeral to a decimal
number. The program should read a Roman numeral. You
may read it as a string or one character at a time. Do
the conversion and then output the decimal number.
Here are the “letters” you need to know:
Symbol = Value
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1,000
iostream
using namespace std;
(declare two arrays of 10)
char array1[10]
int array2[10]
int i
module 1
void convert()
while loop
If array[i] == "the roman numeral value"( example m)
array[i] = "What the value represents for (example 1000)
(repeat the above two lines as necessary but change values)
i++
endwhile
end module 1
void main()
declare int n as 0, total as 0, totals
i = 0
prompt for roman number
input roman number
call to module 1
while array2[n] != NULL
if array2[n] >=array2[n+1]
total = total + array2[n]
n++
end if
else if array2[n]<array2[n+1]
x = array2[n+1] - array2[n]
n++
end else if
endwhile
cout totals
end main
1. Code the pseudocode design as written into C++.
2. Develop test data to prove whether or not the design works. You may use the Roman Numeral-Decimal calculator at this website to create your test data http://www.csgnetwork.com/csgromancnv.html (Links to an external site.) Include test data that tests for all the subtractive exception only values ( 4, 9, 40, 90, 400, and 900, which are written as IV, IX, XL, XC, CD, and CM respectively ) as well as the additive cases. Summarize your results - the summary can be very simple. "I coded and tested your design it (does or doesn't work)."
4. Submit the source code, test data that shows if the code works or not, and your summary for the design
Explanation / Answer
Actually you are given algorithm .but i just the change the part i.e,finding value of roman character.remaing things also done with small modifications.
// Program to convert Roman Numerals to Numbers
#include<bits/stdc++.h>
using namespace std;
// This function returns value of a Roman symbol
int value(char sym)
{
if (sym == 'I')
return 1; //function is for returning decimal value for roman character
if (sym == 'V')
return 5;
if (sym == 'X')
return 10;
if (sym == 'L')
return 50;
if (sym== 'C')
return 100;
if (sym == 'D')
return 500;
if (sym== 'M')
return 1000;
}
// Returns decimal value of roman numaral
int romanToDecimal(string &str)
{
// Initialize result
int totals = 0;
// Traverse given input
for (int i=0; i<str.length(); i++)
{ //iterating loop upto input roman string length
int s1 = value(str[i]); //finding decimal value for roman character
if (i+1 < str.length())
{
int s2 = value(str[i+1]); //getting next value of roman character present in string
// Comparing both values
if (s1 >= s2) //comparing both values
{
// Value of current symbol is greater
// or equal to the next symbol
totals = totals + s1; //adding roman value
}
else
{
totals= totals+ s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
totals= totals + s1;
i++;
}
}
return totals; //returing totals value i.e,decimal value of roman string
}
// Driver Program
int main()
{
string str ; //declaring string str for roman value
cout<<"enter roman value:";
cin>>str; //user input roman value
cout << "Integer value of given roman value is:"
<< romanToDecimal(str) << endl; //output displaying
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.