Archived

This topic is now archived and is closed to further replies.

This topic is 6113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have to write a program that converts Numbers to Roman Numerals. Now I have already done it but I want to know if there is an easier way to program it. I would like not to have to use pointers in my program could some please show me how this would be done. And yes this is for school #include #include #include void main () { int decimal; char romanDigits[8] = "mdclxvi"; char* currentDigit = romanDigits; int romanValues[8] = {1000, 500, 100, 50, 10, 5, 1}; int* currentValue = romanValues; /* longest Roman numeral < 5000 has 19 digits. */ char roman[20] = "-------------------"; /* hyphens are ddebugging */ char* currentRoman = roman; printf ("Please type a positive integer less than 5000: "); scanf ("%d", &decimal); if (decimal <= 0) { exit(1); } while (decimal > 0) { if (decimal >= *currentValue) { *(currentRoman++) = *currentDigit; decimal = decimal - *currentValue; } else { currentDigit++; currentValue++; } } *currentRoman = 0; printf("Roman numeral equivalent = %s\n", roman); }

Share this post


Link to post
Share on other sites
These notations are equivalent:

  
char *pSomeString = "This is just a string";
int x = 3;
char currentCharacter;

// these are equivalent

currentCharacter = *(pSomeString + x);
currentCharacter = pSomeString[x];


Instead of using pointer arithmetic (incrementing a pointer to go to the next character) you can use an index, like x in the above code, and increment that instead. You then access the string through pSomeString[x].

You still need char* pointers for the strings of course, but if you find code full of pointer arithmetic hard to read then the array subscript notation might be easier.

Harry.

Share this post


Link to post
Share on other sites
Two things:

First, rather than having CurrentValue being a pointer, i would use it as an index.
ie:

int CurrentValue = 0

if(decimal>=romanValues[CurrentValue])
{
*(currentRoman++)=currentDigit[CurrentValue];
decimal = decimal - romanValues[CurrentValue];
} else { currentValue++;
}

notice that you can use this as in index into both the currentvalue and the current digit ?

The other thing is that your implementation is wrong:
it would come up with xxviiii for 19, when it should be xix.
and iiii for 4, when it should be iv.
ie, you can only have 3 of one character in a row (except for m)
so you could add a check to see if it''s more than 3 times the current Romanvalue

hope that helps

Wyzfen

Share this post


Link to post
Share on other sites
Not really related to the subject, but just nit-picking that may come up useful to you as time goes on on this board, typing (for example)

#include <iostream.h>   /* If you typed just as shown in the message textbox...*/

...the "<" and ">" wouldn't show up, because they're interpreted as HTML tags, so type < and > instead of < and > =o)

(o= erydo =o)



Edited by - cliffhawkens on March 15, 2001 8:31:01 PM

Share this post


Link to post
Share on other sites