10. Write a program to calculate a temperature provided by the user into a diffe
ID: 3736387 • Letter: 1
Question
10. Write a program to calculate a temperature provided by the user into a different unit system. The program should ask the user to enter a temperature in units of degrees Fahrenheit. The program should then ask the user to choose a unit system they would like to have the value converted into, using a menu. The menu should display the choices "deg C", "K", and "deg R", which should be stored in a cell array. The program should display the output in a sentence formatted as shown below. The actual values in the sentence should change appropriately as the user input and menu selection changes. Choose a final unit deg C deg R Sample Input/Output Enter the temperature [deg F]:-129 Menu Choice: K The equivalent temperature to -129 deg P is 184 K. You are to write this program twice, using two different methods as indicated below. Note that in neither case may you use conditional statements, even if you know how to do so.
Explanation / Answer
%%%%
%a
m={'deg C' ,'K','deg R'};
t=input('Enter the temperature [deg F]: ');
choice=menu('Choose a final unit',m);
newT=[(t-32)/1.8;(t+459.67)*5/9;t+459.67];
fprintf('The equivalent temperature to %i deg F is %i %s. ',t,round(newT(choice)),m{choice})
%%%%%
%b
m={'deg C' ,'K','deg R'};
t=input('Enter the temperature [deg F]: ');
choice=menu('Choose a final unit',m);
conversion={@(t)(t-32)/1.8;@(t)(t+459.67)*(5/9);@(t)t+459.67};
newT=conversion{choice}(t);
fprintf('The equivalent temperature to %i deg F is %i %s. ',t,round(newT),m{choice})
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.