Homework Problem Week 5 Problem 1

3 views (last 30 days)
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
James Tursa
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
Emily Lim on 7 Aug 2015
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. :)

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 6 Aug 2015
Edited: James Tursa on 6 Aug 2015
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

More Answers (1)

Steven Lord
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 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!