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

Write a program in \"C\" language. The roman number program above works correctl

ID: 644123 • Letter: W

Question

Write a program in "C" language. The roman number program above works correctly, provided the user enters valid roman digits followed by a newline at the prompt. If the user enters an invalid roman digit (even spaces), the programs behaves somewhat reasonably (What does it do?), but we would like to make it better.

To do this, you will need access to the files for the roman number program. Then run the command:

We would like the program to be tolerent of white space (spaces and tabs, and still provide the result if all of the other characters are valid roman digits.

If the user enters invalid characters, we would like the function to return 0 as a special value indicating an error instead of a partial result so far. We can use 0 as a special value because there is no Roman number representation for 0 (the Romans didn't understand 0) so 0 would not be returned for normal input. The driver should print an error message when this occurs and let the user try entering another number. You may modify any of the files that you need to, but you will need to break the link and copy the file for those you want to change.

Explanation / Answer

int romanToInt(string s) {
map<char, int> roman;
roman['M'] = 1000;
roman['D'] = 500;
roman['C'] = 100;
roman['L'] = 50;
roman['X'] = 10;
roman['V'] = 5;
roman['I'] = 1;

int res = 0;
for (int i = 0; i < s.size() - 1; ++i)
{
if (roman[s[i]] < roman[s[i+1]])
res -= roman[s[i]];
else
res += roman[s[i]];
}
res += roman[s[s.size()-1]];
return res;
}

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