1 / 11

Exam 1 Review: Part 2

Exam 1 Review: Part 2. Practice Programming. Programming Example Bags Fly Fee !!!. XYZ is a commercial airline that asked you to create a MATLAB program that can calculate the baggage fees for their passengers. They charge $10 for every bag and $.10 for every pound. Program. # of bags.

Télécharger la présentation

Exam 1 Review: Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exam 1 Review: Part 2 Practice Programming

  2. Programming ExampleBags Fly Fee!!! XYZ is a commercial airline that asked you to create a MATLAB program that can calculate the baggage fees for their passengers. They charge $10 for every bag and $.10 for every pound. Program # of bags Total Charge Total Weight in lb

  3. Any ideas …… • Do we know how to solve manually? • Write Algorithm… • Code using MATLAB • Test. clc clear % collect the inputs from the user (#of bags&total weight) NmbrBags = input('How many bags you have? '); TotalWeight = input('Enter the total weight : '); % Calculate the charge=#of bags*10+Total weight*.10) Charge = NmbrBags*10 + TotalWeight*.10; % Print the total charge fprintf('Your total charge is %.2f dollars \n',Charge);

  4. Test, Test, Test….

  5. Life is easy!.. No it is not! • There are other constraints: • First class customers DON’T pay for baggage fees. (display $0.00) • Business and Economy pay according to the formula given before. • Regardless of the class (First/Business/Economy), no passenger is allowed: • More than 3 bags. (3 is okay but not more) • Total weight exceeding 100 lb. (100 lb. is okay but not more) • Give an error message, if any of the 2 conditions happen. • What if, by mistake, we have 0 bags and a non zero value for total weight?!!!! Propose a solution (Give an error message). Program Total Charge # of bags Total Weight in lb Error Message Class Error Message

  6. clear clc % collect the inputs from the user (bags,weight,class) NmbrBags = input('How many bags you have? '); TotalWeight = input('Enter the total weight: '); Class = input('Enter 1=First, 2=Business, 3=Economy: '); %SWITCH Decide what class they are flying if Class == 1 % In case of First, Check the limits if NmbrBags > 3 || TotalWeight > 100 %if true, error msg fprintf ('You are over the limit, can''t fly!'); else%if not then 0.00 dollars fprintf ('Your total charge is 0.00 dollars \n'); end else if NmbrBags > 3 || TotalWeight > 100 fprintf ('You are over the limit, can''t fly!\n'); % check for invalid inputs elseif NmbrBags == 0 && TotalWeight ~= 0 % print error msg fprintf('Check your inputs!\n'); else% Calculate the total charge Charge = NmbrBags*10 + TotalWeight*.10; % Print the total charge fprintf('Your total charge is %.2f dollars \n',Charge); end end

  7. How about we ask the user to enter the number of bags and check if the input is zero or not, that’s before we give the prompt to enter the total weight. Because regardless of the class that the customer is traveling, 0 bags = 0 dollars. This way if the user put 0 for bags, they will not be prompted to enter the weight. Ask for the weight, and the class, then continue…. If bags =0 False True Print total is 0.00

  8. clear clc % check number of bags NmbrBags = input('How many bags you have? '); if NmbrBags == 0 && TotalWeight ~0 fprintf ('Check your inputs \n'); else TotalWeight = input('Enter the total weight: '); Class = input('Enter 1=First, 2=Business, 3=Economy: '); % Check for the limits if NmbrBags > 3 || TotalWeight > 100 fprintf ('You are over the limit, can''t fly!\n'); else if Class == 1 % In case of First, Check the limits fprintf ('Your total charge is 0.00 dollars \n'); else Charge = NmbrBags*10 + TotalWeight*.10; % Print the total charge fprintf('Your total charge is %.2f dollars \n',Charge); end end end Undefined variable and unrelated output

  9. Finally clear clc % check number of bags NmbrBags = input('How many bags you have? '); if NmbrBags == 0 fprintf ('Your total charge is 0.00 dollars \n'); else TotalWeight = input('Enter the total weight: '); Class = input('Enter 1=First, 2=Business, 3=Economy: '); % Check for the limits if NmbrBags > 3 || TotalWeight > 100 fprintf ('You are over the limit, can''t fly!\n'); else if Class == 1 % In case of First, Check the limits fprintf ('Your total charge is 0.00 dollars \n'); else Charge = NmbrBags*10 + TotalWeight*.10; % Print the total charge fprintf('Your total charge is %.2f dollars \n',Charge); end end end

  10. Test, Test, Test

  11. How to tackle Exam (1) Good Luck! SLEEP well SHOW-UP for the exam. RUN home STUDY hard

More Related