Proble with exercise in introductory coursera course
1 回表示 (過去 30 日間)
古いコメントを表示
The exercise says:Write a function called generationXYZ that takes as its only input argument one positive integer specifying the year of birth of a person and returns as its only output argument the name of the generation that the person is part of ('X', 'Y', or 'Z ') according to the table below. For births before 1966, return 'O' for Old and for births after 2012, return 'K' for Kid . Remember that to assign a letter to a variable, you need to put it in single quotes, as in: gen = 'X'. Because of this i wrote this code:
function generationXYZ(year)
if year >= 1966 && year <= 1980
fprintf('X\n');
elseif year >= 1981 && year <= 1999
fprintf('Y\n');
elseif year >= 2000 && year <= 2012
fprintf('Z\n');
elseif year < 1966
fprintf('O\n');
else
fprintf('K\n');
end
end
I have test it and works ok but the grader gives the follow message : Problem 1 (generationXYZ):
Feedback: Your program made an error for argument(s) 1965
Can u help me please????
0 件のコメント
回答 (2 件)
John D'Errico
2015 年 5 月 21 日
編集済み: John D'Errico
2015 年 5 月 21 日
I think what you are missing is that the function MUST return an output argument.
No place in the problem statement did it say to write this information to the command line. That is what fprintf does.
So change your code to be vaguely like this:
function gen = generationXYZ(year)
if year >= 1966 && year <= 1980
gen = 'X';
elseif year >= 1981 && year <= 1999
gen = 'Y';
elseif year >= 2000 && year <= 2012
gen = 'Z';
elseif year < 1966
gen = 'O';
else
gen = 'K';
end
end
You need to understand what it means to return an argument from a function.
"and returns as its only output argument the name"
A function that will return an argument has that variable on the left side of the = in the header.
Your essential logic was valid in the use of an if statement with else clauses. (Ok, you could also have used a switch/case construct, or several other schemes I can think of. There are always many ways to solve any problem.)
0 件のコメント
Star Strider
2015 年 5 月 21 日
I don’t see a problem with the logic, and when I run it, it returns the correct answer. The only thing I can think of is that the grader wants it without the '\n', (although I agree that’s how I would write it).
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!