Need help with this question I was asked to preform

1 view (last 30 days)
Create a script file that will prompt the user for a mass in kg, and then prompt the user again if they would like to convert the mass to ‘lbm’ (for pounds mass) or ‘slugs’ (for slugs). The script should contain a switch statement with two cases and fprintf to the command window the corresponding mass in the units specified by the user. The format of your mass output should be 3 places after the decimal point. Also, include an error statement if neither case is selected (hint: use otherwise). You can type “help error” in the command window to understand how to create an error statement. For example, your command window might contain these input and output statements: Enter the mass in kilograms (kg): 10 Do you want to convert to ‘lbm’ or ‘slugs?: slugs The mass is 0.685 slugs.
  3 Comments
Lucas Kimmel
Lucas Kimmel on 19 Feb 2016
Yes I understand that. I guess I should of been more specific with what exactly I need help with. I am having trouble with the switch statement and I'm not exactly sure how it works
jgg
jgg on 19 Feb 2016
Edited: jgg on 19 Feb 2016
But basically, you give it a switching variable, then provide a series of cases. In your example, you'd want to switch on the input string that the user provided (call it units). Then, your switch statement would look like this:
switch units
case 'lbs'
%do stuff
disp('chose lbs')
case 'slugs'
%do stuff
disp('chose slugs')
otherwise
%throw an erro
disp('chose error')
end
You still need to choose what to do in each case, and handle the input and errors properly.

Sign in to comment.

Answers (1)

MHN
MHN on 19 Feb 2016
Edited: MHN on 19 Feb 2016
We are not going to give you the answer, since this is a basic question which is trying to teach you not only how should you code in Matlab but also how should you learn a programming language! Instead, let me help you with the bigger picture, which is how should you solve such questions! Go to Google, and search "switch matlab", then the top one is: http://www.mathworks.com/help/matlab/ref/switch.html Read the documentation and the examples and you would definitely learn how "switch" works. Otherwise, you would never learn it if we provide the answer.

Categories

Find more on Install Products 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!