MATLAB HELP The following table shows the price for renting a car at a car renta
ID: 1766705 • Letter: M
Question
MATLAB HELP
The following table shows the price for renting a car at a car rental company: Duration Sedan SUV of rent Daily rate [days] Free miles Cost of Daily rate Free miles Cost of per dayadditiona[] per day additional 69.99 59.99 30 or more 39.99 100 120 140 miles [$] 0.69 0.59 0.39 89.99 79.99 59.99 100 120 140 miles [$] 0.89 0.79 0.59 1-10 11-29 Write a MATLAB program in a script file HW5P4.m that calculates the cost of renting a car based on the shown price schedule. The program must prompt the user to enter the type of car (Sedan or SUV), the number of days, and the number of miles driven. The program then displays the cost (roun the rent is xxxx. xx $ . " where XXXXXX is the cost value in $ for the rent in a sentence that reads: "The cost of Run the program three times for the following cases (a) Sedan, 12 days, 1069 miles (b) SUV, 32 days, 6,096 miles. (c) Sedan, 3 days, 511 miles. Hint: Use a switch-case structure for the type of the car (Sedan/SUV) with nested multiple if structure for the number of days and miles. Use fprintf ) to display the text and data. The data should be displayed in f format with two decimal digitsExplanation / Answer
MATLAB CODE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
type=input('Enter 1 for Sedan and 2 for SUV');
d=input('enter duration of rent in days');
m=input('enter no. of miles driven');
switch type
case 1
if (d>=1)&&(d<=10)
f=100;%%%%free miles/day
if m<=f*d %%%%%f*d is total no. of free miles
rent=69.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
elseif m>f*d
rent=(m-f*d)*0.69+69.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
end
elseif (d>=11)&&(d<=29)
f=120;%%%%free miles/day
if m<=f*d
rent=59.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
elseif m>f*d
rent=(m-f*d)*0.59+59.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
end
elseif d>=30
f=140;%%%%free miles/day
if m<=f*d
rent=39.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
elseif m>f*d
rent=(m-f*d)*0.39+39.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
end
end
case 2
if (d>=1)&&(d<=10)
f=100;%%%%free miles/day
if m<=f*d
rent=89.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
elseif m>f*d
rent=(m-f*d)*0.89+89.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
end
elseif (d>=11)&&(d<=29)
f=120;%%%%free miles/day
if m<=f*d
rent=79.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
elseif m>f*d
rent=(m-f*d)*0.79+79.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
end
elseif d>=30
f=140;%%%%free miles/day
if m<=f*d
rent=59.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
elseif m>f*d
rent=(m-f*d)*0.59+59.99*d;
formatSpec='The cost of rent is %4.2f $ ';
fprintf(formatSpec,rent);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
RESULT:
>> Untitled5
Enter 1 for Sedan and 2 for SUV1
enter duration of rent in days12
enter no. of miles driven1069
The cost of rent is 719.88 $
>> Untitled5
Enter 1 for Sedan and 2 for SUV2
enter duration of rent in days32
enter no. of miles driven6096
The cost of rent is 2873.12 $
>> Untitled5
Enter 1 for Sedan and 2 for SUV1
enter duration of rent in days3
enter no. of miles driven511
The cost of rent is 355.56 $
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.