Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MATLAB function called centuries that takes a positive integer smaller t

ID: 3805052 • Letter: W

Question

Write a MATLAB function called centuries that takes a positive integer smaller than or equal to 3000 representing a year as its input and returns a char vector with the century the given year falls into. If the input is invalid, the function returns the empty char vector '' (there is no space between the apostrophes). Centuries are specified using roman numerals. Note that we require

the shortest legal roman number. For a complete list, refer to: http://www.romannumerals.co/roman-numerals-1-to-30. Note that a century goes from year 1 to 100, so for example, the XXth century ended on December 31st, 2000. As an example, the call

>> cent = centuries(1864); will make cent equal to ‘XIX’.

Explanation / Answer

function [cent]=centuries(num)

% validation of input as number or not
if size(num) == [1 1]
answer = isreal(num) && isnumeric(num) && round(num) == num && num >0 && num<=3000 ;
else
answer = false;
end

if(answer==0 )
cent='';
return ;
end

% to check condition for input values equal to 3000
if(num==3000)
cent='XXX';
return ;
end

% to check condition for input values less than 101
if (num<=100)
cent='I';
return;
end

roman={'I','II','III','IV','V','VI','VII','VIII','IX','X'};

y=floor(num/100);
y1=floor(y/10); % get thousands place value
y2=rem(y,10); % get hundreds place value

% to check ones and tens place value is equal to 0
last2=rem(num,100);
if(last2==0)
y2=y2-1;
end

x1='';
x2='';

% to write roman value of thousands place value
if(y1>0 )
for i=1:y1
temp=roman(10);
x1=strcat(x1,temp);
end
end

% to write roman value of hundreds place value
if(y2>=0)
x2=roman(y2+1) ;
end

cent=char(strcat(x1,x2));

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote