Write a short comment explaining the code:
equipment = ["Power generator", "Transformer", "Stepper motor", "DC motor"];
price = [30, 10, 15, 8.5];
n = 4;
tot_number = 0;
tot_cost = 0;
for i=1:n
number = input("Enter number of " + equipment(i) + ": ");
tot_number = tot_number + number;
tot_cost = tot_cost + number * price(i);
end
avg_cost = tot_cost /tot_number;
fprintf("\n");
fprintf("The total number of equipment: %d\n", tot_number);
fprintf("The total cost: £%.2f\n", tot_cost);
fprintf("The average cost £%.2f per item\n", avg_cost);
close all,
clear all,
clc,
%Prepare the equipment list
equipment = {'Power generator', 'Transformer', 'Stepper motor', 'DC motor'};
%Prepare the equipment price list
price = [30, 10, 15, 8.5];
%Get the no. of equipments in a list
n = length(equipment);
%Initialize total equipment to zero
tot_number = 0;
%Initialize total cost to zero
tot_cost = 0;
%Initialize a for loop to get inputs from user
for i=1:n
%Make a string to get input of each equipment
s = strcat('Enter number of ',32, equipment{i},': ');
%Present the list and get input
number = input(s);%'Enter number of ' + equipment(i,1) + ': ');
%Get the total number of equipments
tot_number = tot_number + number;
% Make the total cost
tot_cost = tot_cost + number * price(i);
end
%Get average cost
avg_cost = tot_cost /tot_number;
%print in command window all texts
fprintf('\n');
fprintf('\nThe total number of equipment: %d\n', tot_number);
fprintf('\nThe total cost: £%.2f\n', tot_cost);
fprintf('\nThe average cost £%.2f per item\n', avg_cost);
fprintf('\n\n');
Matlab Output
Enter number of Power generator:2
Enter number of Transformer:3
Enter number of Stepper motor:4
Enter number of DC motor:5
The total number of equipment: 14
The total cost: £192.50
The average cost £13.75 per item
>>
Comments
Leave a comment