Having trouble with some homework.

2 views (last 30 days)
Taylor
Taylor on 9 Oct 2014
Commented: Star Strider on 9 Oct 2014
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.

Answers (3)

Star Strider
Star Strider on 9 Oct 2014
Edited: Star Strider on 9 Oct 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 Comments
Taylor
Taylor on 9 Oct 2014
We tried that but it outputs like
"16.000000 years
12.000000 years
15.000000 years
13.000000 years
22.000000 years
25.858000 years
832.000000 weeks624.000000 weeks780.000000 weeks676.000000 weeks1144.000000 weeks1344.616000 weeks
5824.000000 days4368.000000 days5460.000000 days4732.000000 days8008.000000 days9412.312000 days
139776.000000 hours104832.000000 hours131040.000000 hours113568.000000 hours192192.000000 hours225895.488000 hours
8386560.000000 minutes6289920.000000 minutes7862400.000000 minutes6814080.000000 minutes11531520.000000 minutes13553729.280000 minutes
503193600.000000 seconds377395200.000000 seconds471744000.000000 seconds408844800.000000 seconds691891200.000000 seconds813223756.800005 seconds"
Is there a way for it to only output 1 number each for years, weeks, days, seconds, minutes, and hours?
Star Strider
Star Strider on 9 Oct 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.

Sign in to comment.


Chad Greene
Chad Greene on 9 Oct 2014
Edited: Chad Greene on 9 Oct 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?

Chad Greene
Chad Greene on 9 Oct 2014
Your age variable is given in units of days. So age_year = age/365.25 and age_day should equal age.

Community Treasure Hunt

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

Start Hunting!