Homework Problem Week 5 Problem 1

Don't know what is missing or wrong with my function.
Question asked to 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'.
Generation Born
X 1966-1980
Y 1981-1999
Z 2000-2012
This is my solution:
function gen = generationXYZ(a)
if a < 1996
gen = 'O';
elseif a >= 1996 && a <= 1980
gen = X;
elseif a >= 1981 && a <= 1999
gen = 'Y';
elseif a >= 2000 && a <= 2012
gen = 'Z';
else a > 2012
gen = 'K';
end
and the feedback says there is error for argument 1965 and 1966.
Please point out or guide me on my mistake.

8 个评论

"Remember that to assign a letter to a variable, you need to put it in single quotes, as in: gen = 'X'."
"... For births before 1966, return 'O' for Old ..."
Ouch ...

I sympathize James ;-)

Right. Thanks. Honest mistake. Don't be nasty, I'm new, learning and trying.
Well that's okay...if you don't mind our nickname for you youngsters. We in the "Old" generation call those born in the 80's and 90's, not Generation "X", but "Generation XL". Can you guess why? (Think clothing.) Better change that code of yours below, James.
;-)
Cedric
Cedric 2015-8-7
编辑:Cedric 2015-8-7
Emily, the "Ouch" and "I sympathize" were not about your code/attempt, which is good, but it is tough to see "Old" in your statement for those of us who were born before 66 ;-)
Emily, this was all in good humor. No offense was meant or should be taken. (Although I would suggest 'E' for Experienced!)
And I will point out, as Cedric did, that your question was well written and you made a good attempt, which was why I answered and pointed out your bugs. This is, in fact, quite refreshing on this forum as opposed to others who just post their homework question verbatim without any attempt. When you make a valid attempt, you will get lots of help on this forum. So keep doing what you are doing and don't worry about offending us.
Thumbs up for the explanation, advice and help. Sorry I can be very oblivious. Okay, no offense taken. Yeah, I decided to actually try and learn to do it by myself. No point signing up for the course just to cheat for answers. Will continue to attempt the questions. :)

请先登录,再进行评论。

 采纳的回答

Change this:
else a > 2012
to this:
else
And change this:
elseif a >= 1996 && a <= 1980
gen = X;
to this:
elseif a >= 1966 && a <= 1980 % typo 1996 changed to 1966
gen = 'X'; % you forgot the quotes
And this:
if a < 1996
to this
if a < 1966 % another 1996 typo

更多回答(1 个)

In addition to what the others have said, you can simplify your code a little by adding one check at the beginning.
if isnan(a) % Note "a == NaN" would not work; use ISNAN to detect NaN
error('You did not tell us what to do for a birth in year NaN');
end
NaN is often used, particularly in statistical applications, to represent missing data. Once you've added that check to filter out any missing data, you can be certain that when you get into your IF/ELSEIF/END tree the variable a contains a number between -Inf and Inf inclusive. Then continuing your tree:
if a < 1966
gen = 'O';
elseif ...
If you proceed past this IF condition to the ELSEIF, you now know that a >= 1966 (if you hadn't checked for NaN ahead of time, then NaN would also get you to the ELSEIF since NaN < 1966 is false) because otherwise you would have already classified the person as belonging to Generation O. That means you don't actually need to check that a >= 1966 in your ELSEIF. You can use the same reasoning to simplify all your ELSEIF conditions.

类别

帮助中心File Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by