Help! Indexing a Vector, Display Issues
Show older comments
I asked a question earlier about this problem, and I have almost finished the code, but I am struggling with a couple more things that I have no clue (or experience with MATLAB) to solve. My problem is:
Create a script that asks the user repeatedly for two time intervals in the form of hours, minutes, and seconds, and provides the addition of these two time intervals in the same form. The script should end if the input from the user is zero for all six values. You should write a function which takes in the given time intervals and returns their addition. The script will handle the user input, call the function, and display the corresponding output.
I am, somehow, supposed to convert the time intervals so that, for example, if the total number of minutes entered by the user is 76 minutes, it will convert this to 1 hour and 16 minutes and add 1 hour to the “hours portion” of the vector and 16 to the “minutes portion” of the vector. This needs to happen for the “seconds portion” of the vector as well. I was thinking that I needed to index the vector somehow, but I am not sure how I can do that in this situation.
I also need to get my output to display as hms1 + hms2 = hmsTotal, which I have been struggling with for hours. I haven’t learned any techniques for this as of yet.
Here is my function code:
function [hmsTotal] = intervalAddition(hms1,hms2)
% This function adds the first and second time intervals input by the user.
hmsTotal = (hms1 + hms2);
end
Here is my main script:
clc
clear
flag = true;
while flag
disp('Do you want to enter a time interval?')
answer = input('Enter 1 for YES and 0 for NO :')
if answer == 1
hms1 = input("Enter the first time interval using 6 digits representing hours, minutes, and seconds in vector form: ");
hms2 = input("Enter the second time interval using 6 digits representing hours, minutes, and seconds in vector form: ");
if hms1 == 0 & hms2 == 0
flag = false;
else
intervalAddition(hms1,hms2);
hmsTotal = [hms1 + hms2];
disp(hms1);
disp(hms2);
disp(hmsTotal);
end
else
flag = false;
end
end
1 Comment
Rik
on 27 Oct 2020
If you were to do this with pen and paper, what would you do?
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!