B
billbaitsg
Hi, I need some help with figuring out why my program is all messed
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman
(dec2rom).
int rom2dec(char rom[])
{
//Local Variables
int a = 0; //counter
int dec = 0;//decimal equivalent
//Statements
while(rom[a] != '\0')
{
if(rom[a] >= rom[a+1])
{
dec += rom[a];
}
else
{
dec += (rom[a+1] - rom[a]);
a++;
}
a++;
}//end of while loop
return dec;
}//end of rom2dec
this function basically adds up the values of each roman numeral in
the array and it also checks for subractive numbers (i.e. IV = 4).
The program does not need to worry about numbers that dont make sense
like IIIIX. I must be accessing the character array wrongly because
when I enter a roman numeral of 'I' (one), it calculates the decimal
as 73.
The second part of the program is a huge amount of if's and for's.
void dec2rom(int dec, char rom[])
{
//Local Variables
int count = 0; //counter
int loop = 0; //loop counter
int pos = 0; //position on array
//Statements
if( (dec/M) >= 1 )
{
count = dec/M;
dec -= (count * M);
pos += count;
for(loop = 0; loop < count; loop++)
{
rom[loop] = 'M';
}//end of for loop
}
else if( (dec/C) == 9)
{
rom[pos] = 'C';
rom[pos + 1] = 'M';
pos += 2;
}
else if( (dec/D) >= 1 )
{
count = dec/D;
dec -= (count * D);
rom[pos] = 'D';
pos += count;
}
else if( (dec/C) >= 1 )
{
count = dec/C;
dec -= (count * C);
for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'C';
}//end of for loop
pos += count;
}
else if( (dec/X) == 9)
{
rom[pos] = 'X';
rom[pos + 1] = 'C';
pos += 2;
}
else if( (dec/L) >= 1 )
{
count = dec/L;
dec -= (count * L);
rom[pos] = 'L';
pos += count;
}
else if( (dec/X) >= 1 )
{
count = dec/X;
dec -= (count * X);
for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'X';
}//end of for loop
pos += count;
}
else if( (dec/I) == 9)
{
rom[pos] = 'I';
rom[pos + 1] = 'X';
pos += 2;
}
else if( (dec/I) == 4)
{
rom[pos] = 'I';
rom[pos + 1] = 'V';
pos += 2;
}
else if( (dec/V) >= 1 )
{
count = dec/V;
dec -= (count * V);
rom[pos] = 'V';
pos += count;
}
else if( (dec/I) >= 1 )
{
count = dec/I;
dec -= (count * I);
for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'I';
}//end of for loop
pos += count;
}
rom[pos] = '\0';
return;
}//end of dec2rom
This function should work it's way through a number from the thousands
and end with the smallest numbers. however, if an input of 5329 is
put in, it comes back with 'MMMMM' and doesnt have anything lower than
1000.
I appreciate any suggestions and anyone telling me that I'm an idiot
because I'm accessing character arrays wrongly (if that's my problem)
=)
thx.
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman
(dec2rom).
int rom2dec(char rom[])
{
//Local Variables
int a = 0; //counter
int dec = 0;//decimal equivalent
//Statements
while(rom[a] != '\0')
{
if(rom[a] >= rom[a+1])
{
dec += rom[a];
}
else
{
dec += (rom[a+1] - rom[a]);
a++;
}
a++;
}//end of while loop
return dec;
}//end of rom2dec
this function basically adds up the values of each roman numeral in
the array and it also checks for subractive numbers (i.e. IV = 4).
The program does not need to worry about numbers that dont make sense
like IIIIX. I must be accessing the character array wrongly because
when I enter a roman numeral of 'I' (one), it calculates the decimal
as 73.
The second part of the program is a huge amount of if's and for's.
void dec2rom(int dec, char rom[])
{
//Local Variables
int count = 0; //counter
int loop = 0; //loop counter
int pos = 0; //position on array
//Statements
if( (dec/M) >= 1 )
{
count = dec/M;
dec -= (count * M);
pos += count;
for(loop = 0; loop < count; loop++)
{
rom[loop] = 'M';
}//end of for loop
}
else if( (dec/C) == 9)
{
rom[pos] = 'C';
rom[pos + 1] = 'M';
pos += 2;
}
else if( (dec/D) >= 1 )
{
count = dec/D;
dec -= (count * D);
rom[pos] = 'D';
pos += count;
}
else if( (dec/C) >= 1 )
{
count = dec/C;
dec -= (count * C);
for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'C';
}//end of for loop
pos += count;
}
else if( (dec/X) == 9)
{
rom[pos] = 'X';
rom[pos + 1] = 'C';
pos += 2;
}
else if( (dec/L) >= 1 )
{
count = dec/L;
dec -= (count * L);
rom[pos] = 'L';
pos += count;
}
else if( (dec/X) >= 1 )
{
count = dec/X;
dec -= (count * X);
for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'X';
}//end of for loop
pos += count;
}
else if( (dec/I) == 9)
{
rom[pos] = 'I';
rom[pos + 1] = 'X';
pos += 2;
}
else if( (dec/I) == 4)
{
rom[pos] = 'I';
rom[pos + 1] = 'V';
pos += 2;
}
else if( (dec/V) >= 1 )
{
count = dec/V;
dec -= (count * V);
rom[pos] = 'V';
pos += count;
}
else if( (dec/I) >= 1 )
{
count = dec/I;
dec -= (count * I);
for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'I';
}//end of for loop
pos += count;
}
rom[pos] = '\0';
return;
}//end of dec2rom
This function should work it's way through a number from the thousands
and end with the smallest numbers. however, if an input of 5329 is
put in, it comes back with 'MMMMM' and doesnt have anything lower than
1000.
I appreciate any suggestions and anyone telling me that I'm an idiot
because I'm accessing character arrays wrongly (if that's my problem)
=)
thx.