Write a program which uses three unique user-defined functions to do the followi
ID: 3642172 • Letter: W
Question
Write a program which uses three unique user-defined functions to do the following conversions:1. tonne (t) which is a metric ton to pounds (lbs)
2. pounds (lbs) to ounces (oz)
3. ounces (oz) to grains (gr)
Use a single script m-file to get initial input from the user and to call each of the user-defined functions you created. Within the script m-file, prompt the user to input a weight in metric tons (t or tonnes). Once you get this value, your script m-file should call each user-defined function to get the equivalent values in pounds, ounces, and grains. Then display the initial input in metric tons and the equivalent value in pounds, ounces, and grains. Run your script file using 6.35 tonne. Submit your M-files and a screenshot of your command window showing the results of your test case. You will need the following conversions:
1 tonne = 2,204.62 pound
1 pound = 16.00 ounce
1 ounce = 437.50 grain
Explanation / Answer
% test.m
% Test driver to demonstrate usage of conversion functions
% tonnes2pounds, pounds2ounces, and ounces2grains
%
% prompt for input
tonnes = input('Enter weight in metric tons: ');
% call conversion functions
pounds = tonnes2pounds(tonnes);
ounces = pounds2ounces(pounds);
grains = ounces2grains(ounces);
% display results
fprintf('%8.2f tonnes = %12.2f pounds ', tonnes, pounds);
fprintf('%8.2f tonnes = %12.2f ounces ', tonnes, ounces);
fprintf('%8.2f tonnes = %12.2f grains ', tonnes, grains);
% tonnes2pounds.m
%
function [ p ] = tonnes2pounds( t )
%tonnes2pounds converts tonnes (t) to pounds (p)
% multiply tonnes by 2204.62 to get pounds
p = t * 2204.62;
end
% pounds2ounces.m
%
function [ o ] = pounds2ounces( p )
%pounds2ounces converts pounds (p) to ounces (o)
% multiply pounds by 16.00 to get ounces
o = p * 16.00;
end
% ounces2grains.m
%
function [ g ] = ounces2grains( o )
%ounces2grains converts ounces (o) to grains (g)
% multiply ounces by 437.50 to get grains
g = o * 437.50;
end
Sample Output
>> test
Enter weight in metric tons: 6.35
6.35 tonnes = 13999.34 pounds
6.35 tonnes = 223989.39 ounces
6.35 tonnes = 97995359.00 grains
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.