Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Programming course - question help
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am doing an 'Introduction to MATLAB' course, and one of our tasks is the following:
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'.
X = 1966-1980
Y = 1981-1999
Z = 2000-2012
I have written the following function:
function generationXYZ(n)
if 0 <= n && n <= 1966
fprintf('O\n')
elseif 1966 <= n && n <= 1980
fprintf('X\n')
elseif 1981 <= n && n <= 1999
fprintf('Y\n')
elseif 2000 <= n && n <= 2012
fprintf('Z\n')
elseif n >= 2013
fprintf ('K\n')
else
fprintf ('Year of birth must be greater than 0\n')
end
However when I run the grader, I am marked incorrectly because:
Feedback: Your function made an error for argument(s) 1965
But when I input 1965 as the argument to my function, the output is O, which is correct?
Anyone have any idea where I might be going wrong?
Thank you!
0 comentarios
Respuestas (3)
Stephen23
el 27 de Oct. de 2015
Editada: Stephen23
el 27 de Oct. de 2015
You are printing some text in the command window using fprintf. The task you were given is not to print text into the command window, but to return a string as an output argument from the function.
You can do this by defining the output variable in the function definition:
function out_string = generationXYZ(n)
and define the variable out_string inside your function.
2 comentarios
Stephen23
el 27 de Oct. de 2015
Editada: Stephen23
el 27 de Oct. de 2015
Define a variable using an equals sign and the variable name on the left:
X = 3;
where X is the variable name that you want to use, and the 3 is the value to assign to it. This value could be string:
Y = 'cat';
I would highly recommend that you work through the introductory tutorials, which are a great way to learn basic MATLAB concepts and usage:
and maybe read this:
Thorsten
el 27 de Oct. de 2015
Define your function with an output argument
function s = generationXYZ(n)
and assign this in your function
if 0 <= n && n <= 1966
s = 'O';
else ... % change other lines accordingly
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!