%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% John Barron, 123456789, barron@uwo.ca
%
% This function convert a decimal number (x)
% in the range [1,3999] into a roman numeral (s).
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [s]=new_decimal2roman(x)
% x is the decimal number
% s is a character string
s=''; % initially s is the null or empty string
% is x in the proper range [1,3999]?
if(x<=0 | x> 3999)
error(sprintf('Fatal error: %d out of range [1,3999]\n',x));
end
% roman numberals and their decimal equivalents
% are stored in 2 parallel arrays, letters and numbers.
% roman numeral in letter(i) has numeric value numbers(i).
% the arrays are ordered biggest to smallest in value
letters={'M' 'CM' 'D' 'CD' 'C' 'XC' 'L' 'XL' 'X' 'IX' 'V' 'IV' 'I'};
numbers=[1000 900 500 400 100 90 50 40 10 9 5 4 1];
% for each number (from largest to smallest), in
% the numbers array, check if x >= to it. If yes, while
% this is true for that number, concatenate the roman
% number letter(i) onto string s and substract the
% numeric value in numbers(i) from x
for i=1:numel(numbers)
while(x>=numbers(i))
s=strcat(s,letters{i}); % + is concatenation here
x=x-numbers(i);
end
end
end