I need assistance with this code here. It calculates avarage use of liquid in da
ID: 3838574 • Letter: I
Question
I need assistance with this code here. It calculates avarage use of liquid in days and hours. Actually, it should be LPG not liquid. This is why the program gives unrealistic figures of finishing 45kg in 10 days for cooking. Besides, the program should allow me to enter duration not usage as it has to compute usage. It should also reset, allow me to enter any number of days at the beginning. Below is the code that has to be modified to answer the above
weight = 45; % initial weight
total_usage=0; % initialisation of variable
for day = 1:1:30 % considering a span of 30 days
fprintf('No. of days = %d ',day)
for hour = 1:1:24 % 24 hours in a day
fprintf('No. of hour = %d ',hour)
hour_usage=input('Enter usage in this hour='); % if not used enter zero.
weight = weight - hour_usage
if weight<=0
fprintf('Cylinder is empty');
return;
end
total_usage = total_usage + hour_usage
hour_avg = total_usage/hour
end
day_avg = total_usage/day
end
Explanation / Answer
The code for the same is as below. Comments are given in the code.
weight = 45; % initial weight in Kg
total_usage=0; % initialisation of variable
day_entered=input('Enter any number of days ='); % user input for number of days
hours_entered=input('Enter number of hours LPG is used ='); % average number of hours per day user uses the LPG
for day = 1:1:day_entered % considering a span of day_entered days
fprintf('No. of days = %d ',day)
for hour = 1:1:hours_entered % hours_entered hours in a day
fprintf('No. of hour = %d ',hour)
hour_usage=0.02; %average usage of LPG for cooking (in Kg) as per internet
weight = weight - hour_usage
if weight<=0
fprintf('Cylinder is empty');
return;
end
total_usage = total_usage + hour_usage
hour_avg = total_usage/hour
end
day_avg = total_usage/day
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.