MATLAB Code for problem below A vector v stores for several
MATLAB Code for problem below.
A vector v stores for several employees of the Green Fuel Cells Corporation their hours worked for one week followed for each by the hourly pay rate. For example, if the variable stores that means the first employee worked 33 hours at $10.50 per hour, the second worked 40 hours at $18 an hour, and so on. Write code that will separate this into two vectors, one that stores the hours worked and another that stores the hourly rates. Then, use the array multiplication operator to create a vector, storing in the new vector the total pay for every employee.Solution
% matlab code to deterine pay for each employee
% given vector
v = [33.000, 10.5000, 40.0000, 18.0000, 20.0000, 7.5000];
% hour vector
hours = [];
% hourly rate vector
rate = [];
% fill the vectors
j = 1;
k = 1;
i = 1;
while (i < length(v))
hours(j) = v(i);
rate(k) = v(i+1);
i = i +2;
j = j +1;
k = k + 1;
end
% determine pay for each employee
total_pay = hours.*rate;
for i=1:length(rate)
fprintf (\"employee %d: \ rate: %f\ hours: %f\ Pay: %f\ \ \",i,rate(i),hours(i),total_pay(i));
end
%{
output:
employee 1:
rate: 10.500000
hours: 33.000000
Pay: 346.500000
employee 2:
rate: 18.000000
hours: 40.000000
Pay: 720.000000
employee 3:
rate: 7.500000
hours: 20.000000
Pay: 150.000000
%}
