Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practice Programming Exam 1 Review: Part 2 1. Programming Example Bags Fly Fee!!! 2 XYZ is a commercial airline that asked you to create a MATLAB program.

Similar presentations


Presentation on theme: "Practice Programming Exam 1 Review: Part 2 1. Programming Example Bags Fly Fee!!! 2 XYZ is a commercial airline that asked you to create a MATLAB program."— Presentation transcript:

1 Practice Programming Exam 1 Review: Part 2 1

2 Programming Example Bags Fly Fee!!! 2 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 Weight in lb Total Charge

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

4 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). 5 Program # of bags Total Weight in lb Total Charge Error Message Class Error Message

6 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

7 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. If bags =0 True Print total is 0.00 False Ask for the weight, and the class, then continue….

8 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 Undefined variable and unrelated output

9 9 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

10 Test, Test, Test 10

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


Download ppt "Practice Programming Exam 1 Review: Part 2 1. Programming Example Bags Fly Fee!!! 2 XYZ is a commercial airline that asked you to create a MATLAB program."

Similar presentations


Ads by Google