alright
so to begin with, can you make me understand the working of this code
please explain me how is the romanise function is working here
OK, so this code is meant to print a number in Roman numerals. So we first have to make sure we understand how Roman numerals work.
Basically, you have a set of symbols: M (1000), D (500), C (100), L (50), X (10), V (10) and I (1). Usually, to read a number you just add all the "digits" together. For example, MMXVII = M + M + X + V + I + I = 1000 + 1000 + 10 + 5 + 1 + 1 = 2017. This is the "simple" method, and we always go from the highest to the lowest symbol.
One twist: commonly, instead of writing 4 as IIII and 9 as VIIII, you write it as IV and IX (smaller digit first, meaning you subtract it from the higher digit instead of adding them). You'll sometimes see the same logic applied to 40 (written XL instead of XXXX), 90 (XC instead of LXXXX), 400 (CD instead of CCCC) and 900 (CM instead of DCCCC), but that's more of a modern convention, and this code doesn't do it.
OK, so how do we write a number in this format? It should be pretty obvious, but let's break down the steps:
You start with the highest symbol (M), and see how many times it "fits" into your number. You repeat it that many times (which can be zero), and then move on to the next symbol, fitting it into the part of the number remaining. So for 2017, we ask "how many times does 1000 go into 2017?" The answer is 2, so we write M twice: MM (2000). We subtract 2000 from 2017, leaving 17. Now we ask the same for D (500), C (100) and L (50). In each case, the answer is 0 (all those numbers are bigger than 17), so we don't write anything. When we get to X (10), the answer is 1, so we write a single X after MM: MMX. We subtract 10 from 17, leaving 7, ask how many times V (5) goes into 7 (once), giving MMXV and leaving 2, and finally ask how many times 1 goes into 2 (twice), giving us MMXVII.
And that's pretty much what the code does. It breaks the task down into handling each symbol, which is done by the
romanise() function. It takes three arguments: the first (
y) is the number you want to write. The second (
k) is the value of the symbol you want to try to fit into that number. The third (
ch) is the character representation of that symbol that you want to print. The function prints the symbol
ch as many times as its value
k fits into the number
y, and returns how much of the number is left over:
j=y/k; // How many times does k fit into y (divide y by k, rounding down)
for(i=1;i<=j;i++) // Print the character ch that many times
return(y-k*j); // Return the part of y that is left over (subtract the value of k, multiplied by the number of times we printed it, j)
The rest of the function deals with the fact that 4 and 9 are written differently as special cases. Should be pretty clear, except for one thing: The return values, (y%9) and (y%4), use an operation called modulo. (Which gives the remainder of an integer division. For example, 17%5 = 2 because 17/5 = 3 and 17 - (3*5) = 2.) They will always be equal to 0, and writing them in this format is a little pointless.
So when we call
romanise() repeatedly, each call will print a certain symbol a certain number of times, and let us know how much of the number remains to be printed. We feed that value into the next function call (it's important to note that since we write
yr = romanise (...) on each line, the value of
yr is changing at each step; it's always the
remaining part of the number the user entered). We repeat until we're down to 1, and at that point the number is complete:
main()
{
int yr;
yr = romanise (yr, 1000, 'm');
yr = romanise (yr, 500, 'd');
yr = romanise (yr, 100, 'c');
yr = romanise (yr, 50, 'l');
yr = romanise (yr, 10, 'x');
yr = romanise (yr, 5, 'v');
yr = romanise (yr, 1, 'i');
}