Having trouble with some homework.
古いコメントを表示
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.
回答 (3 件)
Star Strider
2014 年 10 月 9 日
編集済み: Star Strider
2014 年 10 月 9 日
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 件のコメント
Taylor
2014 年 10 月 9 日
Star Strider
2014 年 10 月 9 日
編集済み: Star Strider
2014 年 10 月 9 日
My pleasure!
The ‘tq’ line is simply an illustration that ‘age_year’ is a string of ASCII characters. Ignore it except to see what it does.
Experiment with datevec instead and see if that helps:
age_year = datevec(now - datenum(birth_year,birth_month,birth_day));
Taylor
2014 年 10 月 9 日
Star Strider
2014 年 10 月 9 日
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
2014 年 10 月 9 日
編集済み: Chad Greene
2014 年 10 月 9 日
0 投票
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
2014 年 10 月 9 日
0 投票
Your age variable is given in units of days. So age_year = age/365.25 and age_day should equal age.
カテゴリ
ヘルプ センター および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!