
Concept explainers
To Write:
A menu-driven program that will read in an employee data base for a company from a file and do specified operations on the data. The file stores the following information for each employee:
• Name
• Department
• Birth Date
• Date Hired
• Annual Salary
• Office Phone Extension
You are to decide exactly how this information is to be stored in the file. Design the layout of the file, and then create a sample data file in this format to use when testing your program. The format of the file is up to you. However, space is critical. Do not use any more characters in your file than you have to! Your program is to read the information from the file into a data structure, and then display a menu of options for operations to be done on the data. You may not assume in your program that you know the length of the data file. The menu options are:
1. Print all of the information in an easy-to-read format to a new file.
2. Print the information for a particular department.
3. Calculate the total payroll for the company (the sum of the salaries).
4. Find out how many employees have been with the company for N years (N might be 10, for example).
5. Exit the program.

Answer to Problem 31E
Solution:
The script is saved and executed as follows.
Explanation of Solution
MATLAB Code:
%MATLAB code to create only considers years.
%
function nyears(emps)
%Define the function to create only considers years.
hiredyears = zeros(1, length(emps));
for i = 1:length(emps)
hiredyears(i) = str2num(emps(i).hired(5:6)) + 1900;
end
current_year = 2013;
N = 20;
fy = find(current_year-hiredyears >= N);
fprintf('%d employees have worked at least %d years.\n\n', length(fy), N)
end
%end of function
%The script file should be placed in the same folder.
%MATLAB code to prints the menu of options and error-checks.
%Function file
function choice_selection = options
%Define the function to prints the menu of options and error-checks.
%until the user pushes one of the buttons
choice_selection = menu('Choose an option', 'Print all', 'Print dept', 'Payroll', 'N years', 'Exit Program');
%Define the statemnet to check the condition if the user closes the menu box rather than
%pushing one of the buttons, choice will be 0.
while choice_selection == 0
disp('Error-please choose one of the options.')
choice_selection = menu('Choose an option', 'Print all', 'Print dept', 'Payroll', 'N years', 'Exit Program');
end
end
%end of function
%The script file should be placed in the same folder.
%MATLAB code to calculate the employee payroll.
%Function file
function payroll(salaries)
fprintf('The total of the salaries is $%.2f.\n\n', ...
sum(salaries))
end
%end of function
%The script file should be placed in the same folder.
%MATLAB code to write to screen; could change to write to file.
%Function file
function printall(emp)
%Define the function to write to screen; could change to write to file.
fprintf('%-15s%-8s%11s%11s %-10s %5s\n\n', 'Name', 'Dept', ...
'Birth Date', 'Hire Date', 'salary', 'Phone')
for i = 1:length(emp)
fprintf('%-15s%-8s', emp(i).name, emp(i).dept)
b = emp(i).birth;
birthdate = sprintf('%s-%s-19%s', b(1:2), b(3:4), b(5:6));
h = emp(i).hired;
hiredate = sprintf('%s-%s-19%s', h(1:2), h(3:4), h(5:6));
fprintf('%11s%11s', birthdate, hiredate)
fprintf('$%9.2f x%s\n', emp(i).salary, emp(i).phone)
end
end
%end of function
%The script file should be placed in the same folder.
%MATLAB code to check the condition if the user closes the menu box rather than pushing one of the buttons, choice will be 0.
%Function file
function printdept(emp)
choice_selection = menu('Choose Dept', 'sales','service', 'Trucking');
%Define the instruction to check the condition if the user closes the menu box rather than
%pushing one of the buttons, choice will be 0.
while choice_selection == 0
disp('Error-please choose one of the options.')
choice_selection = menu('Choose Dept', 'sales', 'service', 'Trucking');
end
ca = {'sales','service','Trucking'};
chosen = ca{choice_selection};
fprintf('%-15s%-8s%11s%11s %-10s %5s\n\n', 'Name', 'Dept','Birth Date', 'Hire Date', 'salary', 'Phone')
for i = 1:length(emp)
if strcmp(emp(i).dept, chosen)
fprintf('%-15s%-8s', emp(i).name, emp(i).dept)
b = emp(i).birth;
birthdate = sprintf('%s-%s-19%s', b(1:2), b(3:4), b(5:6));
h = emp(i).hired;
hiredate = sprintf('%s-%s-19%s', h(1:2), h(3:4), h(5:6));
fprintf('%11s%11s', birthdate, hiredate)
fprintf('$%9.2f x%s\n', emp(i).salary, emp(i).phone)
end
end
end
%end of function
%The script file should be placed in the same folder.
%MATLAB code to read the employees.
%Function file
function emp = reademployees
%Define the function to read the employees.
emp(2).name = 'Akhilesh, Abhishek';
emp(2).dept = 'service';
emp(2).birth = '072267';
emp(2).hired = '121298';
emp(2).salary = 87333;
emp(2).phone = '5388';
emp(1).name = 'Mohit, Roy';
emp(1).dept = 'sales';
emp(1).birth = '072267';
emp(1).hired = '121288';
emp(1).salary = 77333;
emp(1).phone = '5389';
%Define the instruction to create the employee name, departemnt,
%birth date, hired department, salary and phone number.
end
%end of function
%The script file should be placed in the same folder.
%....................Start of the script file.............................
employees = reademployees;
%Define the instrcution to creates an employee data base for a company and
%performs some operations on the data.
Choice_Selection = options;
%Define the instruction to read the info from a file.
while Choice_Selection ~= 5
switch Choice_Selection
%Define the instruction to call a function to display a menu and get choice
case 1
printall(employees)
%Define the instruction to prints all of the info to a file.
case 2
printdept(employees)
%Define the instruction to prints info for one department.
case 3
payroll([employees.salary])
%Define the instruction to prints total payroll.
case 4
nyears(employees)
%Define the instruction to prints employees >= N years.
end
Choice_Selection = options;
%Define the instruction to display menu again and get user's choice.
end
Save the MATLAB script with name, chapter_9_54793_9_33E.m in the current folder. Execute the script by typing the string name at the command window to write A menu-driven program that will read in an employee data base for a company from a file and do specified operations on the data. The file stores the following information for each employee:
• Name
• Department
• Birth Date
• Date Hired
• Annual Salary
• Office Phone Extension
You are to decide exactly how this information is to be stored in the file. Design the layout of the file, and then create a sample data file in this format to use when testing your program. The format of the file is up to you. However, space is critical. Do not use any more characters in your file than you have to! Your program is to read the information from the file into a data structure, and then display a menu of options for operations to be done on the data. You may not assume in your program that you know the length of the data file. The menu options are:
1. Print all of the information in an easy-to-read format to a new file.
2. Print the information for a particular department.
3. Calculate the total payroll for the company (the sum of the salaries).
4. Find out how many employees have been with the company for N years (N might be 10, for example).
Result:
Therefore, the script is saved and executed.
Want to see more full solutions like this?
Chapter 9 Solutions
Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
- 4. (5 pts) Conduct a chi-square contingency test (test of independence) to assess whether there is an association between the behavior of the elderly person (did not stop to talk, did stop to talk) and their likelihood of falling. Below, please state your null and alternative hypotheses, calculate your expected values and write them in the table, compute the test statistic, test the null by comparing your test statistic to the critical value in Table A (p. 713-714) of your textbook and/or estimating the P-value, and provide your conclusions in written form. Make sure to show your work. Did not stop walking to talk Stopped walking to talk Suffered a fall 12 11 Totals 23 Did not suffer a fall | 2 Totals 35 37 14 46 60 Tarrow_forwardQuestion 2 Parts manufactured by an injection molding process are subjected to a compressive strength test. Twenty samples of five parts each are collected, and the compressive strengths (in psi) are shown in Table 2. Table 2: Strength Data for Question 2 Sample Number x1 x2 23 x4 x5 R 1 83.0 2 88.6 78.3 78.8 3 85.7 75.8 84.3 81.2 78.7 75.7 77.0 71.0 84.2 81.0 79.1 7.3 80.2 17.6 75.2 80.4 10.4 4 80.8 74.4 82.5 74.1 75.7 77.5 8.4 5 83.4 78.4 82.6 78.2 78.9 80.3 5.2 File Preview 6 75.3 79.9 87.3 89.7 81.8 82.8 14.5 7 74.5 78.0 80.8 73.4 79.7 77.3 7.4 8 79.2 84.4 81.5 86.0 74.5 81.1 11.4 9 80.5 86.2 76.2 64.1 80.2 81.4 9.9 10 75.7 75.2 71.1 82.1 74.3 75.7 10.9 11 80.0 81.5 78.4 73.8 78.1 78.4 7.7 12 80.6 81.8 79.3 73.8 81.7 79.4 8.0 13 82.7 81.3 79.1 82.0 79.5 80.9 3.6 14 79.2 74.9 78.6 77.7 75.3 77.1 4.3 15 85.5 82.1 82.8 73.4 71.7 79.1 13.8 16 78.8 79.6 80.2 79.1 80.8 79.7 2.0 17 82.1 78.2 18 84.5 76.9 75.5 83.5 81.2 19 79.0 77.8 20 84.5 73.1 78.2 82.1 79.2 81.1 7.6 81.2 84.4 81.6 80.8…arrow_forwardName: Lab Time: Quiz 7 & 8 (Take Home) - due Wednesday, Feb. 26 Contingency Analysis (Ch. 9) In lab 5, part 3, you will create a mosaic plot and conducted a chi-square contingency test to evaluate whether elderly patients who did not stop walking to talk (vs. those who did stop) were more likely to suffer a fall in the next six months. I have tabulated the data below. Answer the questions below. Please show your calculations on this or a separate sheet. Did not stop walking to talk Stopped walking to talk Totals Suffered a fall Did not suffer a fall Totals 12 11 23 2 35 37 14 14 46 60 Quiz 7: 1. (2 pts) Compute the odds of falling for each group. Compute the odds ratio for those who did not stop walking vs. those who did stop walking. Interpret your result verbally.arrow_forward
- Solve please and thank you!arrow_forward7. In a 2011 article, M. Radelet and G. Pierce reported a logistic prediction equation for the death penalty verdicts in North Carolina. Let Y denote whether a subject convicted of murder received the death penalty (1=yes), for the defendant's race h (h1, black; h = 2, white), victim's race i (i = 1, black; i = 2, white), and number of additional factors j (j = 0, 1, 2). For the model logit[P(Y = 1)] = a + ß₁₂ + By + B²², they reported = -5.26, D â BD = 0, BD = 0.17, BY = 0, BY = 0.91, B = 0, B = 2.02, B = 3.98. (a) Estimate the probability of receiving the death penalty for the group most likely to receive it. [4 pts] (b) If, instead, parameters used constraints 3D = BY = 35 = 0, report the esti- mates. [3 pts] h (c) If, instead, parameters used constraints Σ₁ = Σ₁ BY = Σ; B = 0, report the estimates. [3 pts] Hint the probabilities, odds and odds ratios do not change with constraints.arrow_forwardSolve please and thank you!arrow_forward
- Solve please and thank you!arrow_forwardQuestion 1:We want to evaluate the impact on the monetary economy for a company of two types of strategy (competitive strategy, cooperative strategy) adopted by buyers.Competitive strategy: strategy characterized by firm behavior aimed at obtaining concessions from the buyer.Cooperative strategy: a strategy based on a problem-solving negotiating attitude, with a high level of trust and cooperation.A random sample of 17 buyers took part in a negotiation experiment in which 9 buyers adopted the competitive strategy, and the other 8 the cooperative strategy. The savings obtained for each group of buyers are presented in the pdf that i sent: For this problem, we assume that the samples are random and come from two normal populations of unknown but equal variances.According to the theory, the average saving of buyers adopting a competitive strategy will be lower than that of buyers adopting a cooperative strategy.a) Specify the population identifications and the hypotheses H0 and H1…arrow_forwardYou assume that the annual incomes for certain workers are normal with a mean of $28,500 and a standard deviation of $2,400. What’s the chance that a randomly selected employee makes more than $30,000?What’s the chance that 36 randomly selected employees make more than $30,000, on average?arrow_forward
- What’s the chance that a fair coin comes up heads more than 60 times when you toss it 100 times?arrow_forwardSuppose that you have a normal population of quiz scores with mean 40 and standard deviation 10. Select a random sample of 40. What’s the chance that the mean of the quiz scores won’t exceed 45?Select one individual from the population. What’s the chance that his/her quiz score won’t exceed 45?arrow_forwardSuppose that you take a sample of 100 from a population that contains 45 percent Democrats. What sample size condition do you need to check here (if any)?What’s the standard error of ^P?Compare the standard errors of ^p n=100 for ,n=1000 , n=10,000, and comment.arrow_forward
- Glencoe Algebra 1, Student Edition, 9780079039897...AlgebraISBN:9780079039897Author:CarterPublisher:McGraw HillHolt Mcdougal Larson Pre-algebra: Student Edition...AlgebraISBN:9780547587776Author:HOLT MCDOUGALPublisher:HOLT MCDOUGALBig Ideas Math A Bridge To Success Algebra 1: Stu...AlgebraISBN:9781680331141Author:HOUGHTON MIFFLIN HARCOURTPublisher:Houghton Mifflin Harcourt
- Elementary Geometry For College Students, 7eGeometryISBN:9781337614085Author:Alexander, Daniel C.; Koeberlein, Geralyn M.Publisher:Cengage,Functions and Change: A Modeling Approach to Coll...AlgebraISBN:9781337111348Author:Bruce Crauder, Benny Evans, Alan NoellPublisher:Cengage LearningCollege Algebra (MindTap Course List)AlgebraISBN:9781305652231Author:R. David Gustafson, Jeff HughesPublisher:Cengage Learning





