Proble with exercise in introductory coursera course

2 views (last 30 days)
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????

Answers (2)

John D'Errico
John D'Errico on 21 May 2015
Edited: John D'Errico on 21 May 2015
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.)

Star Strider
Star Strider on 21 May 2015
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).

Categories

Find more on Downloads in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!