I have written the following Code which will convert an integer number to a roman..I have a array list of integer number which I pass through a for loop and the return value is individual converted Roman character..
My Issue is when the converted value is returned from the function I want to capture it in another character array one after the other but am failed to do it..
plzzzz Help

void intToRoman(int num[1000],int m) 
{

	char * roman[100] = {0};
	int iTemp,kTemp,toRoman[1000],jTemp;
	int reqNum = {0};    	

	for(iTemp = 0;iTemp< m-1;iTemp++)
		{
			if(num[iTemp] > num[iTemp+1])
					{
						break;
					}
		}
		int i=0;
		//printf("%d",iTemp+1);
		for(kTemp=0;kTemp<=iTemp;kTemp++)
		{
		roman[kTemp] =	Number_AsRomanString(num[kTemp]);
			//reqNum[kTemp] = num[kTemp];
                //when I print here I get the converted values properly     
		printf("%s",roman[kTemp]);
		printf("\n");
		
		}

               //when I Re Print here I get redundant Values of the last index     
		for(int j=0;j<=iTemp;j++)
		{
			printf("%s",roman[j]);
			printf("\n");
		}

	//	ReWritingIntoTheFile(iTemp);
}

char *Number_AsRomanString( int iNumber )
{
struct RomanDigit_t
  {
char *m_psString;
int m_iValue;
  };

static const RomanDigit_t RomanDigits[]=
  {
    {"M",  1000},
    {"CM",  900},
    {"D",   500},
    {"CD",  400},
    {"C",   100},
    {"XC",   90},
    {"L",    50},
    {"XL",   40},
    {"X",    10},
    {"IX",    9},
    {"V",     5},
    {"IV",    4},
    {"I",     1},
  };

static char sRomanString[20];
sRomanString[0] = '\0';

for (int i=0; iNumber && i<sizeof(RomanDigits)/
                           sizeof(RomanDigits[0]); i++)
  {
while ( RomanDigits[i].m_iValue <= iNumber )
    {
strcat( sRomanString, RomanDigits[i].m_psString );
iNumber -= RomanDigits[i].m_iValue;
    }
  }
return sRomanString;
}

Recommended Answers

All 2 Replies

I have written the following Code which will convert an integer number to a roman..I have a array list of integer number which I pass through a for loop and the return value is individual converted Roman character..
My Issue is when the converted value is returned from the function I want to capture it in another character array one after the other but am failed to do it..
plzzzz Help

If you're going to return a value you have to avtually return the value. All you did is load a local array with the Roman numerals and exit the function.

Pass the Roman array into the function and load it.

And do not post as a Code Snippet. Requests for help are not snippets.

i think putting a big struct of strings like "CD" and "CM" and values such as 400 and 900 (and so forth) is sloppy way to go about it and overly complicates the thing.

the algorithm behind roman numerals is simple: if the smaller value precedes the larger, then the smaller value is deducted from the larger, rather than added. (Note also that only the even powers of 10 (ie, 1, 10, 100) are ever deducted from a larger value.)

So consider this far more simplified code:

const char RoNum[7] = "MDCLXVI";
const int  RoVal[7] = {1000,500,100,50,10,5,1};

int main(void)
{
    int i; 
    int num = 2037;          // also try num = 1998
    char[32] roman = {'\0'};

    for (i=0; i<7; i++)
    {
        while (num >= RoVal[i])
        {
            num -= RoVal[i];
            strncat(roman,&RoNum[i],1);
        }
        //
        // ... more code here ...
        //
    }
    printf("Number: %d = Roman Number: %s\n\n", num, roman);
    
    return 0;
}

This will work correctly for numbers that don't require smaller values preceding larger, such as num = 2037. But it won't work correctly for numbers such as 1998. And so this only solves part of the problem.

Now the question becomes: how do you determine when a smaller value needs to be deducted from the larger, and so requiring a smaller character to precede the larger?

the solution, to be entered in the code where notated, is not much more complicated than what's already given.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.