Write a program in a script file that calculates the cost of a telephone call ac
ID: 3629679 • Letter: W
Question
Write a program in a script file that calculates the cost of a telephone call according to the following price schedule: The program asks the user to enter the time the call is made (day, evening, or night) and the duration of the call (a number that can have one digit to the right of the decimal point). If the duration of the call is not an integer, the program rounds up the duration to the next integer. The program then displays the cost of the call Run the program three times for the following calls: 8.3min at 1:32 P.M. (A) 34.5 min at 8:00 P.M. (c) 29.6 mm at 1:00 A.M.Explanation / Answer
This code involves using "Input" statements to take user data and nested if-statements. Copy the code below and paste it into an "m-file" called "phone.m". To create an m-file, go to File->New->M-File. Copy this code into it and save it as phone.m. Then at the Matlab prompt, type "phone" to start the program.
format bank;
TimeOfDay = input('What time of day? (0=Day[8am-6pm],1=Evening[6pm-12am], 2=Night[12am-8am]) ');
while (TimeOfDay ~= 0 && TimeOfDay ~= 1 && TimeOfDay ~= 2)
TimeOfDay = input('What time of day? (0=Day[8am-6pm],1=Evening[6pm-12am], 2=Night[12am-8am]) ');
end
Duration = input('How long was the call? (In minutes, to one decimal place) ');
cost = 0;
if (TimeOfDay == 0),
if (Duration < 10),
rate = 0.10;
cost = rate*Duration;
elseif (Duration < 30),
rate = 0.08;
cost = 1 + rate*(Duration-10);
else % Duration > 30
rate = 0.06;
cost = 2.60 + rate*(Duration-30);
end
elseif (TimeOfDay == 1),
if (Duration < 10),
rate = 0.07;
cost = rate*Duration;
elseif (Duration < 30),
rate = 0.05;
cost = 0.70 + rate*(Duration-10);
else % Duration > 30
rate = 0.04;
cost = 1.70 + rate*(Duration-30);
end
else
if (Duration < 10),
rate = 0.04;
cost = rate*Duration;
elseif (Duration < 30),
rate = 0.03;
cost = 0.40 + rate*(Duration-10);
else % Duration > 30
rate = 0.02;
cost = 1.00 + rate*(Duration-30);
end
end
cost
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.