Write a function called roman that takes a string representing an integer between 1 and 20 inclusive using Roman numerals and returns the Arabic equivalent as a number of type uint8. If the input is illegal or its value is larger than 20, roman retur

Write a function called roman that takes a string representing an integer between 1 and 20 inclusive using Roman numerals and returns the Arabic equivalent as a number of type uint8. If the input is illegal or its value is larger than 20, roman retur
function myarabic = roman(n)
switch n
case 'I'
if myarabic == 1
return;
end
case 'II'
if myarabic == 2
return;
end
case 'III'
if myarabic == 3
return;
case 'IV'
if myarabic == 4
return;
case 'V'
if myarabic == 5
return;
case 'VI'
if myarabic == 6
return;
case 'VII'
if myarabic == 7
return;
case 'VIII'
if myarabic == 8
return;
case 'IX'
if myarabic == 9
return;
case 'X'
if myarabic == 10
return;
case 'XI'
if myarabic == 11
return;
case 'XII'
if myarabic == 12
return;
case 'XIII'
if myarabic == 13
return;
case 'XIV'
if myarabic == 14
return;
case 'XV'
if myarabic == 15
return;
case 'XVI'
if myarabic == 16
return;
case 'XVII'
if myarabic == 17
return;
case 'XVIII'
if myarabic == 18
return;
case 'XIX'
if myarabic == 19
return;
case 'XX'
if myarabic == 20
return;
elseif myarabic == uint8;
return;
end
end
grader says solution is not correct can anybody help me advance thanks

답변 (1개)

The ONLY thing your function does is to return - nothing else. Plus, it checks myarabic , which has not even been assigned any value yet, so the first time it checks it will throw an "undefined variable" error. Instead of saying
if myarabic == 17
return;
simply say
myarabic = 17 % Only one equal sign!!!

댓글 수: 6

We would need your current code to comment further.
Try this:
function test
clc; % Clear the command window.
workspace; % Make sure workspace panel is showing.
format long g;
format compact;
myarabic = roman('X')
myarabic = roman('iv')
myarabic = roman('badInput')
function myArabic = roman(romanString)
switch upper(romanString)
case 'I'
myArabic = 1;
case 'II'
myArabic = 2;
case 'III'
myArabic = 3;
case 'IV'
myArabic = 4;
case 'V'
myArabic = 5;
case 'VI'
myArabic = 6;
case 'VII'
myArabic = 7;
case 'VIII'
myArabic = 8;
case 'IX'
myArabic = 9;
case 'X'
myArabic = 10;
case 'XI'
myArabic = 11;
case 'XII'
myArabic = 12;
case 'XIII'
myArabic = 13;
case 'XIV'
myArabic = 14;
case 'XV'
myArabic = 15;
case 'XVI'
myArabic = 16;
case 'XVII'
myArabic = 17;
case 'XVIII'
myArabic = 18;
case 'XIX'
myArabic = 19;
case 'XX'
myArabic = 20;
otherwise
myArabic = 'unknown';
end
still there is a error Your function made an error for argument(s) 'I'
Too many input arguments. Error in roman (line 7) myarabic = roman('X')
You stored all of it in a file named roman.m . Your initial lines need to be stored in a file named after the function you name there, test. You could also store all of it in test.m

댓글을 달려면 로그인하십시오.

태그

질문:

2015년 6월 5일

댓글:

2015년 6월 8일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by