In MATLAB, Write a script file that prompts the user for a row vector, where eac
ID: 3803906 • Letter: I
Question
In MATLAB, Write a script file that prompts the user for a row vector, where each element of the vector is a time in seconds (for example, the use will input the row vector [47, 1000, 5000]). The script file should call a user-defined function multiple times (the number of times the function is called should correspond to the number of elements in the vector).
The user-defined function should accept a single scalar input, time (in seconds); covert that timeinto the equivalent time (in hours, minutes and seconds), and output the hours, minutes, andseconds.
Using the script file, present the outputs from the function to the command window in the formathh:mm:ss. For example, the time 1000 s should be displayed as:
Your input, 1000(s), is equivalent to 0:16:40 (hh:mm:ss)
Explanation / Answer
vec = input ("Enter: "); %for input we do not need any thhing special to accept vectors
secs = mod(vec,60); %We here divide each element of the vector by 60 and store the remainder in secs vector
mins = floor(vec/60); %We here divide each element of the vector by 60 and store the quotient in mins vector
%we stored it in mins so that original values of vec are not tampered, which we need to print
hr = floor(mins/60); %same here
mins = mod(mins,60);
for i=1:numel(vec) %numel counts the number of elements in a vector
printf("Your input, %d(s), is equivalent to %02i:%02i:%02i (hh:mm:ss) ",vec(i), hr(i), mins(i), secs(i));
% %02i determines that 2 characters are printed, not one in case of single digits from 0-9
end
Champ, I hope that this answer will help you to score great marks. In case you need any help, feel free to comment bank, shall get back to you as soon as possible.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.