Writing a script file for cost of a telephone call according to the following price schedule:

The problem is to write a script that will calculate the cost. I understand that it is a loop problem. I'm lost as to how to define the time. I believe that it has to be converted to 24 hr format to use greater than less than etc.. but any help would be grateful.

5 Kommentare

Can you tell roughly what kind of schedule you want to make. For example, if person call for 10 minutes then for the first 5 minutes he is charged 1/min and after that he is charged 0.5/min. Or the schedule depends upon day-time. For example if person call before 12am he is charged 1/min and after 12am he is charged 0.5/am? Can you elaborate your problem
Sorry for not showing the information.
What happens when a person starts a call during the "day" period and ends it up during the "evening" period?
Write a program in a script file that calculates the cost of mailing a small package according to the following price schedule
Write a program in a script file that calculates the cost of mailing a small package according to the following price schedule:
Type of service
Weight more than 0 kg to 1kg
Weight more than 1 kg to 5kg
Ground
RM1.50
RM1.50+RM0.50 for each kg or fraction of a kg above 1kg.
Air
RM3.00
RM3.00+RM0.90 for each kg or fraction of a kg above 1kg.
Overnight
RM18
RM18.00+RM6.00 for each kg or fraction of a kg above 1kg.
The program asks the user to enter the weight and the type of service. The program then displays the cost. If a weight larger than 5kg is entered a message “Service is not available for packages that weigh more than 5kg” is displayed. Run the program and enter 0.25, 3.2, and 10 kg for Ground and Air service, and 1, 4.1 and 6.5 kg for Overnight service.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Dear Andrew, here is the code for your question:
calltime = input('Enter call time(day, evening, night): ', 's');
callduration = input('Enter call duration: ');
callduration = round(callduration);
AboveThirty = 0;
TentoThirty = 0;
OnetoTen = 0;
TotalCost = 0;
if callduration > 30
AboveThirty = callduration - 30;
TentoThirty = 20;
OnetoTen = 10;
elseif callduration > 10 && callduration <= 30
TentoThirty = callduration - 10;
OnetoTen = 10;
else
OnetoTen = callduration;
end
switch calltime
case 'day'
OnetoTen = OnetoTen * 0.1;
if TentoThirty ~= 0
TentoThirty = TentoThirty * 0.08 + 1;
end
if AboveThirty ~= 0
AboveThirty = AboveThirty * 0.06 + 2.6;
end
TotalCost = OnetoTen + TentoThirty + AboveThirty;
case 'evening'
OnetoTen = OnetoTen * 0.07;
if TentoThirty ~= 0
TentoThirty = TentoThirty * 0.05 + 0.7;
end
if AboveThirty ~= 0
AboveThirty = AboveThirty * 0.04 + 1.7;
end
TotalCost = OnetoTen + TentoThirty + AboveThirty;
case 'night'
OnetoTen = OnetoTen * 0.4;
if TentoThirty ~= 0
TentoThirty = TentoThirty * 0.03 + 0.4;
end
if AboveThirty ~= 0
AboveThirty = AboveThirty * 0.02 + 1;
end
TotalCost = OnetoTen + TentoThirty + AboveThirty;
otherwise
disp('Invalid call time')
end
fprintf('Call cost is: %f\n', TotalCost)
I hope it helps. Good luck!

16 Kommentare

dpb
dpb am 29 Okt. 2013
Bearbeitet: dpb am 29 Okt. 2013
Bad form for HW ? to just provide code... :(
Hints and leading suggestions are ok, but just providing answers doesn't really help the student gain much. I'd recommend removing this and providing some hints instead...
Altho there's at least one bug so I suppose the OP had best check it thoroughly before submitting... :)
Thanks for the help. I just needed a better understand of the problem. So there is no way to do it based on time. Only input of day,evening and night?
dpd i know. But there is something in this code which I can tell you is important but missing. You can see it but don't describe it. It is not complete if you read the question description and my answer. lolx so just let it be this way. For rest I will give hint to asker
Andrew try to understand the code then you can make changes by yourself to convert from day, evening and night to time according to your question
Making day,evening and night a set time variable is originally the part I needed help with. I know how to use the if elseif statements.
convert the day, night and evening into 24 hour format and apply conditions on time then. Then it will work as it is doing right now
datestr to convert to 24 hour from 12 hr format.
yes you can use it for conversion. Input time in 12 hr format and convert it to 24 hr format using this function
That's where I ran into my problem. I use this and it produces an array from 1 to 32
show me your code so that I can see the problem
in=input('Enter time of day:')
in=datestr( 'in','HH:MM')
Its probably way wrong.
Here is the correction in it:
in = input('Enter time of day:', 's'); % Take input as string using 's'
in = datestr(in,'HH:MM')
It would probably help if the input was a string. Thanks
yes and in your case input is string indeed
There's still a logic/implementation error as compared to the program requirements...and that one isn't missing, it's just not what was asked for.
yes it happens. Asker will be able to correct it by himself. Thanks for pointing out

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by