exam 1 review: part 2

11
Practice Programming Exam 1 Review: Part 2 1

Upload: david-espinoza

Post on 02-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 2: Exam 1 Review: Part 2

Programming ExampleBags 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

Page 3: Exam 1 Review: Part 2

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

clcclear

NmbrBags = input('How many bags you have? ');TotalWeight = input('Enter the total weight : ');

Charge = NmbrBags*10 + TotalWeight*.10;

Page 5: Exam 1 Review: Part 2

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

Page 6: Exam 1 Review: Part 2

6

clearclc % 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'); endelse 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); endend

Page 7: Exam 1 Review: Part 2

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

FalseAsk for the weight, and the class, then continue….

Page 8: Exam 1 Review: Part 2

8

clearclc % check number of bagsNmbrBags = 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 endend

Undefined variable and unrelated output

Page 9: Exam 1 Review: Part 2

9

clearclc % check number of bagsNmbrBags = 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 endend