Q:

Write a menu-driven program to convert a time in seconds to other units (minutes, hours, and so on). The main script will loop to continue until the user chooses to exit

0

 Write a menu-driven program to convert a time in seconds to other units (minutes, hours, and so on). The main script will loop to continue until the user chooses to exit. Each time in the loop, the script will generate a random time in seconds, call a function to present a menu of options, and print the converted time. The conversions must be made by individual functions (e.g. one to convert from seconds to minutes). All user-entries must be error-checked. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch6Ex27.m

% Menu-driven program to convert a time from seconds

% to other units specified by the user

% Generate a random time in seconds

rantime = randi([1 10000]);

% Call a function to display a menu and get a choice

choice = timeoption;

% Choice 3 is to exit the program

while choice ~= 3

 switch choice

 case 1

 fprintf('%d seconds is %.1f minutes\n',...

 rantime, secsToMins(rantime));

 case 2

 fprintf('%d seconds is %.2f hours\n',...

 rantime, secsToHours(rantime));

 end

 % Generate a random time in seconds

 rantime = randi([1 10000]);

 % Display menu again and get user's choice

 choice = timeoption;

end

timeoption.m

function choice = timeoption

% Print the menu of options and error-check

% until the user pushes one of the buttons

% Format of call: timeoption or timeoption()

% Returns the integer value of the choice, 1-3

choice = menu('Choose a unit', 'Minutes', ...

 'Hours', 'Exit');

% If the user closes the menu box rather than 

% pushing one of the buttons, choice will be 0

while choice == 0

 disp('Error - please choose one of the options.')

 choice = menu('Choose a unit', 'Minutes', ...

 'Hours', 'Exit');

end

end

secsToMins.m

function mins = secsToMins(seconds)

% Converts a time from seconds to minutes

% Format secsToMins(seconds)

% Returns the time in minutes

mins = seconds / 60;

end

secsToHours.m

function hours = secsToHours(seconds)

% Converts a time from seconds to hours

% Format secsToHours(seconds)

% Returns the time in hours

hours = seconds / 3600;

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now