A shopping mall is on sales promotion and the discount is calculated as followed:
Price<200, no discount;
200≤Price<500, 3% off;
500≤Price<1000, 5% off;
1000≤Price<2500, 8% off;
2500≤Price, 10% off.
i. Define a function to calculate the discount when the price is given.
ii. Plot the price-discount curve when price≤5000.
(hint: you can use if…else…,or switch…case…)
(Using MATHLAB/SciLab Programme)
Main script
clc;close all;clear all;
%i
price=5000;
d=saledisc(price);
fprintf('The discount amount for Rs %5.2f is %5.2f\n',price,d)
% ii)
price=[200:1:5000];
d=saledisc(price);
figure;plot(price,d);grid;xlabel('Price');ylabel('Discount');title('Price vs discount plot')
Output:
The discount amount for Rs 5000.00 is 500.00
Function script:
function discount=saledisc(price)
k=0;
for price=price
k=k+1;
if price<200
discount(k)=0;
elseif price>=200 && price<500
discount(k)=price*3/100;
elseif price>=500 && price<1000
discount(k)=price*5/100;
elseif price>=1000 && price<2500
discount(k)=price*8/100;
else
discount(k)=price*10/100;
end
end
end
Get Answers For Free
Most questions answered within 1 hours.