Having trouble with some homework.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
For an assignment, my group was supposed to create a code that would output a person's age in days, weeks, years, months, hours, minutes, and seconds. However, we were unable to get it to work correctly.
The TA for our class said the code was mostly alright but he said there was some issues with the output, in particular with the '%'. We still are having trouble figuring out what to do and the TA seems to be having trouble explaining what we did wrong.
We don't want all our homework done for us or anything, but we would appreciate some help figuring out what exactly we did wrong and some advice for how to fix it.
This is our code:
name = input('What is your name (in single quotes)? ');
birth_year = input('What year were you born? ');
birth_month = input('What month were you born? ');
birth_day = input('What day were you born? ');
age = now - datenum(birth_year,birth_month,birth_day);
age_year = datestr(now - datenum(birth_year,birth_month,birth_day), 'YYYY');
age_week = age_year * 52;
age_day = age_week * 7;
age_hour = age_day * 24;
age_min = age_hour * 60;
age_sec = age_min * 60;
disp(sprintf('%s is officially: ', name))
disp(sprintf('\n%f years', age_year))
disp(sprintf('%f weeks', age_week))
disp(sprintf('%f days', age_day))
disp(sprintf('%f hours', age_hour))
disp(sprintf('%f minutes', age_min))
disp(sprintf('%u seconds', age_sec))
Thanks.
0 Kommentare
Antworten (3)
Star Strider
am 9 Okt. 2014
Bearbeitet: Star Strider
am 9 Okt. 2014
This is the problem:
age_year = datestr(now - datenum(birth_year,birth_month,birth_day), 'YYYY');
It’s returning ‘age_year’ as an ASCII string, not a number.
Add 0 to it and see what you get:
tq = age_year + 0
4 Kommentare
Star Strider
am 9 Okt. 2014
It outputs ‘age_year’ as a vector, with a lot of information you are calculating later. See the documentation for datevec for details.
You will have to rewrite some of your code, but datevec should make your task considerably easier, since it does some of the work for you.
Chad Greene
am 9 Okt. 2014
Bearbeitet: Chad Greene
am 9 Okt. 2014
If you want to go above and beyond, you could look up data for life expectancy based on birth year (could also take into account the country born in, gender, etc), and display a snarky message like,
Your life is approximately 64% over. Have you met at least 64% of your life goals yet?
0 Kommentare
Chad Greene
am 9 Okt. 2014
Your age variable is given in units of days. So age_year = age/365.25 and age_day should equal age.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!