Homework Problem Week 5 Problem 1
Show older comments
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 Comments
dpb
on 6 Aug 2015
"Remember that to assign a letter to a variable, you need to put it in single quotes, as in: gen = 'X'."
James Tursa
on 6 Aug 2015
"... For births before 1966, return 'O' for Old ..."
Ouch ...
Cedric
on 6 Aug 2015
I sympathize James ;-)
Image Analyst
on 7 Aug 2015
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.
;-)
James Tursa
on 7 Aug 2015
Edited: James Tursa
on 7 Aug 2015
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.
Emily Lim
on 7 Aug 2015
Accepted Answer
More Answers (1)
Steven Lord
on 7 Aug 2015
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.
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!