Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,309 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 838 people online right now. Registration is fast and FREE... Join Now!




Need help writing a program that converts a roman numeral into a decim

 
Reply to this topicStart new topic

Need help writing a program that converts a roman numeral into a decim

jovankamcev24
1 Dec, 2008 - 05:05 PM
Post #1

New D.I.C Head
*

Joined: 7 Nov, 2008
Posts: 17

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

User is offlineProfile CardPM
+Quote Post


janotte
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 08:52 PM
Post #2

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,137



Thanked: 150 times
Expert In: C/C++

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 08:57 PM
Post #3

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
Think about the relationship between letters and their numeral equivalents. That will go a long way before you even write one line of code. Planning!
User is offlineProfile CardPM
+Quote Post

jovankamcev24
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 09:18 PM
Post #4

New D.I.C Head
*

Joined: 7 Nov, 2008
Posts: 17

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.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 09:22 PM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
One train of thought...

cpp

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
User is offlineProfile CardPM
+Quote Post

janotte
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 09:42 PM
Post #6

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,137



Thanked: 150 times
Expert In: C/C++

My Contributions
QUOTE(jovankamcev24 @ 1 Dec, 2008 - 09:18 PM) *

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.
User is offlineProfile CardPM
+Quote Post

jovankamcev24
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 09:50 PM
Post #7

New D.I.C Head
*

Joined: 7 Nov, 2008
Posts: 17

QUOTE(KYA @ 1 Dec, 2008 - 09:22 PM) *

One train of thought...

cpp

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
User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 11:01 PM
Post #8

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 123



Thanked: 3 times
Dream Kudos: 150
My Contributions
QUOTE
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....
icon_up.gif
User is offlineProfile CardPM
+Quote Post

jovankamcev24
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 11:09 PM
Post #9

New D.I.C Head
*

Joined: 7 Nov, 2008
Posts: 17

QUOTE(harshakirans @ 1 Dec, 2008 - 11:01 PM) *

QUOTE
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....
icon_up.gif


i dont quite understand the 3rd step... what and why are you doing that?

User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
1 Dec, 2008 - 11:38 PM
Post #10

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 123



Thanked: 3 times
Dream Kudos: 150
My Contributions
OOOh ya,


It was a mistake from my end....

3.value in decimal equals value + the enum value of letter read


But this is was just to help you get started with hands on code....


Anyway this does not solve the question as mentioned pseudo would evaluate XL as 60 which is actually 40 wink2.gif
User is offlineProfile CardPM
+Quote Post

janotte
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
2 Dec, 2008 - 03:34 AM
Post #11

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,137



Thanked: 150 times
Expert In: C/C++

My Contributions
QUOTE(jovankamcev24 @ 1 Dec, 2008 - 09:50 PM) *

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.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
2 Dec, 2008 - 07:44 AM
Post #12

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Lillefix
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
2 Dec, 2008 - 08:02 AM
Post #13

D.I.C Head
Group Icon

Joined: 19 Sep, 2008
Posts: 131



Thanked: 17 times
Dream Kudos: 50
My Contributions
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.

As to the original posters question, I would recommend this link:
http://www.geocities.com/oosterwal/computer/roman.html

User is offlineProfile CardPM
+Quote Post

KYA
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
2 Dec, 2008 - 08:06 AM
Post #14

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
That would have to be taken into account, but there are only a few premade conditions that could be accounted for 'XL', etc...
User is offlineProfile CardPM
+Quote Post

Lillefix
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
2 Dec, 2008 - 08:23 AM
Post #15

D.I.C Head
Group Icon

Joined: 19 Sep, 2008
Posts: 131



Thanked: 17 times
Dream Kudos: 50
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

KYA
RE: Need Help Writing A Program That Converts A Roman Numeral Into A Decim
2 Dec, 2008 - 08:24 AM
Post #16

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
Tough choice there wink2.gif

This post has been edited by KYA: 2 Dec, 2008 - 08:24 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:07PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month