MATLAB Time of day can be represented several ways, among them 12-hour time and
ID: 3685416 • Letter: M
Question
MATLAB
Time of day can be represented several ways, among them 12-hour time and 24-hour time. Write a function which takes as input 24-hour time in hours and minutes and outputs 12-hour time in hours, minutes, and a text string indicating a.m., etc. Rules for use of a.m., etc. can be found in the "times of day" section of the NIST Web Clock Frequently asked questions (FAQ) web site. The function should print an error message if the input value is not a valid time, it should print a warning message if the input value generates an ambiguous result, and it should not print anything if the input is valid and does not generate an ambiguous result. Assume that this function is being used in an application where "noon" is a valid output, e.g. printing out airline flight schedules (this means you should ignore the "shortest measurable duration" part of the FAQ). Include help with the function. Test your function with the following inputs plus two more test cases which you create: (0,0), (0,47), (11,36), (12,0), (12,21). Examples of the function follow (the output has been compacted to save space).
[h1,m1,s1]=convert24to12(1,5)
h1=1 m1=5 s1='a.m.'
[h2,m2,s2=convert24to12(12,0)
h2=12 m2=0 s2='noon'
Explanation / Answer
% function [h1,m1,s1]=convert24to12(1,5) h1=input('enter an hours in between 24 hrs only not greater 24 hrs: '); m1=input('enter an minutes in less than 59 minutes only :'); if h1>12 h1 = h1-12; s1='pm'; disp(s1); elseif h1==12 a=0; s1='noon'; elseif h1<12 fprintf('the value is %d',h1); s1='am'; disp(s1); else fprintf('You have not entered the valid input!! '); end fprintf('the date and time is %d %d %s',h1,m1,s1); % end sample output:: >> mm enter an hours in between 24 hrs only not greater 24 hrs 12 enter an minutes in less than 59 minutes only 0 the date and time is 12 0 noon>> >> mm enter an hours in between 24 hrs only not greater 24 hrs 1 enter an minutes in less than 59 minutes only 5 the value is 1am the value is 1 5 am>> >> mm enter an hours in between 24 hrs only not greater 24 hrs: 22 enter an minutes in less than 59 minutes only :1 the date and time is 10 1 pm>> >> mm enter an hours in between 24 hrs only not greater 24 hrs: 12 enter an minutes in less than 59 minutes only :21 the date and time is 12 21 noon>> >> mm enter an hours in between 24 hrs only not greater 24 hrs: 11 enter an minutes in less than 59 minutes only :36 the date and time is 11 36 am>> mm enter an hours in between 24 hrs only not greater 24 hrs: 0 enter an minutes in less than 59 minutes only :47 the date and time is 0 47 am>> >> mm enter an hours in between 24 hrs only not greater 24 hrs: 0 enter an minutes in less than 59 minutes only :0 the value is 0am the value is 0 0 am>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.