Join 300,482 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,768 people online right now. Registration is fast and FREE... Join Now!
I was given a problem that asks the user to imput a strin of characters which is a roman numeral, and i have to make the program convert the number into decimal . here is the problem discription.
The program prompts the user for a Roman numeral which is entered as a character string. The program first converts the Roman numeral to decimal and then it prints the decimal numeral. The program halts when the user enters a string that is not a valid Roman numeral. Assume that each roman numeral has a length of at most 10. The following table gives the Roman symbols and their decimal equivalents. M-1000 D-500 C-100 L-50 X-10 V-5 I-1
The algorithm for converting a roman numeral is as follows.
R1R2R3....Rn
1. Set i=1 (i is the position of the symbol currently being scanned. ) 2. Set convert=0 (At the end convert will be the decimal value.) 3. If i=n add the decimal value of Rn to convert and stop. 4. If the decimal value of Ri is greater than or equal to the decimal value of Ri+1, add the decimal value of Ri to convert , set i to i+1 and go to step 3. 5. If the decimal value of Ri is less than the decimal value of Ri+1, subtract the decimal value of Ri from convert, set i=i+1 and go to step 3.
now i need a lil push as to give me of an idea where to begin. i will post my code later on when i have a sense of what im doing. i was thinking of approcahing the problem by creating the variables M, D, C, L, X, V, I and then initializing them with their value. Then the user enters the roman numeral into a character array and then comparing each individual cell to the next one. but something tells me its a lot more complicated and it wont work. i need ideas please
We can't write code for you but I can tell you that the majority of CompSci students have had to write a solution to this "Roman to Decimal" problem during their studies so if you Googled around the net with a few different search criteria you are going to find a lot of discussion of the relevant ideas and possible algorithms.
Also remember that some of the people who comment on this will have it wrong (not everyone gets a pass mark on every assignment) so make sure you treat anything you find with caution and use your own judgement on what's good and what's not.
Once you have a solid foundation in code that you 'own' we might be able to help you.
im not asking for you guys to write my code im just merely asking for some advice on how to get started. i cannot find even one discussion on the net with the same format that i use in my C class.
im not asking for you guys to write my code im just merely asking for some advice on how to get started. i cannot find even one discussion on the net with the same format that i use in my C class.
Oh yes I understood that's what you were asking. Sorry if it didn't come across that way.
When I had to do this I got out a bit of paper and worked through the algorithm on various years until I had the idea of how it worked in my mind. ---- If you take an easy example think of the year 2000. This is decimal so it really means: 2x10^3 0x10^2 0x10^1 0x10^0.
We just use the wonderful invention of Islamic scholars that allows us to write that out by understanding that the position we write a number let's us know what exponent it means.
Your teacher has given you a great algorithm that exploits that idea of using the position in the number, as written, to determine what the Roman value is. Does that idea of the position having meaning remind you of anything in C/C++? Perhaps an array?
Try working through your teacher's algorithm on paper for: 2000 1999 1984 1444 paying attention to position and one of the many, many ways to do this may become clearer.
enum {I = 1, V =5, X=10, L=50, C=100, D=500, M = 1000};
M-1000 D-500 C-100 L-50 X-10 V-5 I-1
if i initialize the letters like that, when i scan the users input into a string like if they input V would it automatically be 5? this is a hard problem
if i initialize the letters like that, when i scan the users input into a string like if they input V would it automatically be 5? this is a hard problem
Nope it does not convert automatically, You have to scan through each of letters in the input string and convert them accordingly.
May be you can follow this pseudo-code
1. Input the string from user 2. For each letter in the n'th position of input array 3. value in decimal equals to value(the enum value of letter) * nth power of 10 4. Output the value
Hope you are clear with the methodology to start coding....
if i initialize the letters like that, when i scan the users input into a string like if they input V would it automatically be 5? this is a hard problem
Nope it does not convert automatically, You have to scan through each of letters in the input string and convert them accordingly.
May be you can follow this pseudo-code
1. Input the string from user 2. For each letter in the n'th position of input array 3. value in decimal equals to value(the enum value of letter) * nth power of 10 4. Output the value
Hope you are clear with the methodology to start coding....
i dont quite understand the 3rd step... what and why are you doing that?
if i initialize the letters like that, when i scan the users input into a string like if they input V would it automatically be 5? this is a hard problem
I think it might be time for you to show some work done.
A suggestion, an idea, some code, some pseudocode, anything other than waiting for someone to give you the answer on a plate.
Alternatively you could put this string "convert roman to decimal c++" into Google and choose from the many pages of options that spring up to solve this that you claimed you couldn't find any of a few posts back.
Another idea: use getline to take in the whole string, then have a total variable. Parse each entry and sum the total variable. Print out total. Profit.
KYA, the only problem with you 'simple' solution is that in roman numerals a number which is smaller than the next number is subtracted from the next number.
Not really, with the the seven different figures: I, V, X, L, C, D, M
it would be 36 possibilities where the first was lower than the second. Besides, are we coders whose task it is to find neat solutions or are we lazy bastards?