trying to create a table with entered data

15 views (last 30 days)
Zack
Zack on 26 Apr 2014
Edited: Image Analyst on 14 Aug 2016
Hi everyone I'm attempting to write a program that will make a table out of the data that is entered.
"The CEO of a company has asked you to write a MATLAB program that determines the bonus the employees are going to receive. Employees with performance ratings of 5 get a 4% bonus, those with performance rating of 4 get a 3% bonus, those with performance ratings of 3 get a 2 % bonus and everyone else gets a 1 % bonus. Create a MATLAB program that uses a FOR loop and IF-ELSEIF-ELSE statements. The output should include the total amount of the bonuses are costing the company and all of the employees; with their employee number and new salary. Use the following data to test your program."
Code:
clearvars
num=input('Enter Total Number of employees: ');
for j=1:num %number of employs for 2nd number
emp=input('Enter Employee ID Number: ');
r=input('Enter Employee Rating: ');
sal=input('Enter Employee Salary: ');
if r==5
bonus(j)=sal*1.04;
elseif r==3
bonus(j)=sal*1.02;
elseif r~=5 && r~=3
bonus(j)=sal*1.01;
end;
end
t=uitable
set(t,'Data',magic(num))
set(t,'ColumnWidth',{25})
  • Thus far it will allow me to enter the data and create a table but thats about it.

Answers (3)

Geoff Hayes
Geoff Hayes on 26 Apr 2014
Hi Zack,
Perhaps rather than start with code to get input from the user, start with a sample matrix of data and go from there. For example:
data = [123 5 53000;
124 4 58000;
125 2 51000;
126 4 63000];
In the above, the first column is for the employee id, the second for the employee rating, and the third for the employee salary.
As per the (homework) problem, you will need to iterate over each row (a for loop, i=1:numEmployees), and given the rating ( data(i,2) ) determine the bonus (if/elseif/else conditions) by applying the rating percentage to the salary ( data(i,3) ) similar to what you have done above. Then the new salary can be appended to a (new) fourth column of data (or a copy of data). Once completed for each employee, the difference between the sums of the fourth (new salaries) and third (old salary) columns will be the bonus that the company must pay out.
Geoff

Walter Roberson
Walter Roberson on 26 Apr 2014
The bonus is not the salary times 1.04 (or 1.03 or 1.02 or 1.01): that would give you the salary with the bonus added. You just want the 4% (0.04) and so on.
You need to be outputting the employee numbers, and that implies you need to keep a record of them.
The "new salary" for each employee is the same as their original salary, as bonuses are one-time payments, not increases in salaries.

Ifra Riaz
Ifra Riaz on 14 Aug 2016
Edited: Image Analyst on 14 Aug 2016
  1. In the matrix vac_days each column represents the number of vacations days an employee took in the last 3 months.
  2. In the matrix salaries each column represents that employee’s salary in the last 3 months.
  3. Correct the salaries so that employees that took less then 3 days of vacation in a certain month get a 10% bonus to their salary.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!