Question

In: Computer Science

Write a recursive function that converts Old Roman Numeral to base 10 values.

C++ program

Write a recursive function that converts Old Roman Numeral to base 10 values. Print out the Old Roman Number and it base 10 equivalents. You will read all the digits of a Roman number into an array of characters. You will process each Roman digit in the array using ONLY POINTERS. Putting a ‘\0’ at the end of the input characters allows you to print the string easier.

Use the following input to test your function:    DVIII, MMMDCCCCLXXXXIIII, DCLXVI,   CCCLXXXVII

Solutions

Expert Solution

//without using map,

#include

using namespace std;
int character_used(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
  
return -1;
}
int mainFunctionworking(string& myArray)
{
// initialize sum to zero
int sum = 0;
  
// Traverse given input
for (int i = 0; i < myArray.length(); i++) {
// Getting character_used of symbol s[i]
int s1 = character_used(myArray[i]);
  
if (i + 1 < myArray.length()) {
// Getting character_used of symbol s[i+1]
int s2 = character_used(myArray[i + 1]);
  
// Comparing both character_used
if (s1 >= s2) {
// character_used of current symbol
// is greater or equal to
// the next symbol
sum = sum + s1;
}
else {
// character_used of current symbol is
// less than the next symbol
sum = sum + s2 - s1;
i++;
}
}
else {
sum = sum + s1;
}
}
return sum;
}
  
// Driver Program
int main()
{
// Considering inputs given are valid
string myArray = "DVIII";
cout << "Decimal equivalent for "<<myArray<<" is "<< mainFunctionworking(myArray) << endl;
myArray = "MMMDCCCCLXXXXIIII";
cout << "Decimal equivalent for "<<myArray<<" is "<< mainFunctionworking(myArray) << endl;
myArray = "DCLXVI";
cout << "Decimal equivalent for "<<myArray<<" is "<< mainFunctionworking(myArray) << endl;
  
myArray = "CCCLXXXVII";
cout << "Decimal equivalent for "<<myArray<<" is "<< mainFunctionworking(myArray) << endl;
  
return 0;
}

 


Related Solutions

in Java Write a function that inputs a 4 digit year and outputs the ROMAN numeral...
in Java Write a function that inputs a 4 digit year and outputs the ROMAN numeral year M is 1,000 C is 100 L is 50 X is 10 I is 1 Test with 2016 and 1989
Write a simple java program to list roman numeral for a given range of numbers. Roman...
Write a simple java program to list roman numeral for a given range of numbers. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 Roman numerals are usually written largest to smallest from left to right. But a number like 4 is not written as IIII. It is written as IV. Because...
Using PHP, design a function that given a Roman numeral (Links to an external site.)Links to...
Using PHP, design a function that given a Roman numeral (Links to an external site.)Links to an external site. in input, is able to compute the modern Hindu–Arabic numeral (Links to an external site.)Links to an external site. system representation (aka, 0123456789). For example: Input Output VI 6 IV 4 MCMXC 1990 IX 9 Be sure to add all the necessary checks and at least one testing function. Try to split the logic in more small functions.
Using PHP, design a function that given a Roman numeral (Wikipedia link.), in input, is able...
Using PHP, design a function that given a Roman numeral (Wikipedia link.), in input, is able to compute the modern Hindu–Arabic numeral (wikipedia link), system representation (aka, 0123456789). For example: Input Output VI 6 IV 4 MCMXC 1990 IX 9 Be sure to add all the necessary checks and at least one testing function. Try to split the logic in more small functions.
1.what is a base condition of recursive function? 2. why is the base condition of recursive...
1.what is a base condition of recursive function? 2. why is the base condition of recursive function important?
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT