This code does not show that cylinder is empty: WEIGHT<=0 when\" 0\" is entered.
ID: 2083664 • Letter: T
Question
This code does not show that cylinder is empty: WEIGHT<=0 when" 0" is entered. How can I shorten the outputs, that is avarage hours,days, KGs of gas usage
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
Explanation / Answer
We first check the weight before the loop and if weight is 0, we will display that weight is 0 and exit. Again in the loop, first we will check for weight and then will continue decreasing the weight for each hour and day