I have calculated costs from each floor but now I need to calculate total cost for all floors how would I do this?

1 view (last 30 days)
This code all works but Im not sure how to combine each floors total cost to get the overall building cost.(Im sorry its long.)
choice9 = questdlg('Would you like to calculate costs for spaces?','question','yes','no','yes');
switch choice9
case 'no'
disp('not calculating cost')
case 'yes'
ResidentialCost=input('Please enter the building cost for residential spaces per m^2: ');
OfficeCost=input('Please enter the building cost for office spaces per m^2: ');
EducationalCost=input('Please enter the building cost for educational spaces per m^2: ');
ToiletCost=input('Please enter the building cost for toilet spaces per m^2: ');
StorageCost=input('Please enter the building cost for Storage spaces per m^2: ');
if ResidentialArea<=0 || ResidentialCost<=0
TotalResidentialCost=0;
disp('No cost for residential spaces')
else
TotalResidentialCost= ResidentialCost*ResidentialArea;
disp("Your total cost for floor "+c+" residential spaces is:"+TotalResidentialCost)
end
if OfficeArea<=0 || OfficeCost<=0
TotalOfficeCost=0;
disp('No cost for office spaces')
else
TotalOfficeCost= OfficeCost*OfficeArea;
disp("Your total cost for floor "+c+" office spaces is:"+TotalOfficeCost)
end
if EducationalArea<=0 || EducationalCost<=0
TotalEducationalCost=0;
disp('No cost for educational spaces')
else
TotalEducationalCost= EducationalCost*EducationalArea;
disp("Your total cost for floor "+c+" educational spaces is:"+TotalEducationalCost)
end
if ToiletArea<=0 || ToiletCost<=0
TotalToiletCost=0;
disp('No cost for educational spaces')
else
TotalToiletCost= ToiletCost*ToiletArea;
disp("Your total cost for floor "+c+" toilet spaces is:"+TotalToiletCost)
end
if StorageArea<=0 || StorageCost<=0
TotalStorageCost=0;
disp('No cost for storage spaces:')
else
TotalStorageCost= StorageCost*StorageArea;
disp("Your total cost for floor "+c+" storage spaces is:"+TotalStorageCost)
end
OverallCost=0;
OverallCost=TotalStorageCost+TotalToiletCost+TotalEducationalCost+TotalOfficeCost+TotalResidentialCost+OverallCost;
disp("Your running total for floor "+c+" is: "+OverallCost)
end
end

Answers (1)

Umur Ayberk
Umur Ayberk on 5 May 2022
It is hard to tell without seeing the rest of the code but I believe you can use a variable to store the total cost of floors from 1 to c outside the loop and keep adding the overall floor cost to it.
% I assume there is some way to input the number of floors and areas for
% each floor but I just used sample values here.
nFloors = 3;
RunningBuildingCost = 0;
for c = 1:nFloors
ResidentialArea = 1;
OfficeArea =1;
EducationalArea = 1;
ToiletArea = 1;
StorageArea = 1;
%%
choice9 = questdlg('Would you like to calculate costs for spaces?','question','yes','no','yes');
switch choice9
case 'no'
disp('not calculating cost')
case 'yes'
ResidentialCost=input('Please enter the building cost for residential spaces per m^2: ');
OfficeCost=input('Please enter the building cost for office spaces per m^2: ');
EducationalCost=input('Please enter the building cost for educational spaces per m^2: ');
ToiletCost=input('Please enter the building cost for toilet spaces per m^2: ');
StorageCost=input('Please enter the building cost for Storage spaces per m^2: ');
if ResidentialArea<=0 || ResidentialCost<=0
TotalResidentialCost=0;
disp('No cost for residential spaces')
else
TotalResidentialCost= ResidentialCost*ResidentialArea;
disp("Your total cost for floor "+c+" residential spaces is:"+TotalResidentialCost)
end
if OfficeArea<=0 || OfficeCost<=0
TotalOfficeCost=0;
disp('No cost for office spaces')
else
TotalOfficeCost= OfficeCost*OfficeArea;
disp("Your total cost for floor "+c+" office spaces is:"+TotalOfficeCost)
end
if EducationalArea<=0 || EducationalCost<=0
TotalEducationalCost=0;
disp('No cost for educational spaces')
else
TotalEducationalCost= EducationalCost*EducationalArea;
disp("Your total cost for floor "+c+" educational spaces is:"+TotalEducationalCost)
end
if ToiletArea<=0 || ToiletCost<=0
TotalToiletCost=0;
disp('No cost for educational spaces')
else
TotalToiletCost= ToiletCost*ToiletArea;
disp("Your total cost for floor "+c+" toilet spaces is:"+TotalToiletCost)
end
if StorageArea<=0 || StorageCost<=0
TotalStorageCost=0;
disp('No cost for storage spaces:')
else
TotalStorageCost= StorageCost*StorageArea;
disp("Your total cost for floor "+c+" storage spaces is:"+TotalStorageCost)
end
OverallCost=0;
OverallCost=TotalStorageCost+TotalToiletCost+TotalEducationalCost+TotalOfficeCost+TotalResidentialCost+OverallCost;
disp("Your running total for floor "+c+" is: "+OverallCost)
end
%%
RunningBuildingCost = RunningBuildingCost + OverallCost;
disp("Your running total for floors upto "+c+" is: "+RunningBuildingCost)
end

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!