
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
- In a group of 40 people, 35% have never been abroad. Two people are selected at random without replacement and are asked about their past travel experience. a. Is this a binomial experiment? Why or why not? What is the probability that in a random sample of 2, no one has been abroad? b. What is the probability that in a random sample of 2, at least one has been abroad?arrow_forwardQuestions An insurance company's cumulative incurred claims for the last 5 accident years are given in the following table: Development Year Accident Year 0 2018 1 2 3 4 245 267 274 289 292 2019 255 276 288 294 2020 265 283 292 2021 263 278 2022 271 It can be assumed that claims are fully run off after 4 years. The premiums received for each year are: Accident Year Premium 2018 306 2019 312 2020 318 2021 326 2022 330 You do not need to make any allowance for inflation. 1. (a) Calculate the reserve at the end of 2022 using the basic chain ladder method. (b) Calculate the reserve at the end of 2022 using the Bornhuetter-Ferguson method. 2. Comment on the differences in the reserves produced by the methods in Part 1.arrow_forwardTo help consumers in purchasing a laptop computer, Consumer Reports calculates an overall test score for each computer tested based upon rating factors such as ergonomics, portability, performance, display, and battery life. Higher overall scores indicate better test results. The following data show the average retail price and the overall score for ten 13-inch models (Consumer Reports website, October 25, 2012). Brand & Model Price ($) Overall Score Samsung Ultrabook NP900X3C-A01US 1250 83 Apple MacBook Air MC965LL/A 1300 83 Apple MacBook Air MD231LL/A 1200 82 HP ENVY 13-2050nr Spectre XT 950 79 Sony VAIO SVS13112FXB 800 77 Acer Aspire S5-391-9880 Ultrabook 1200 74 Apple MacBook Pro MD101LL/A 1200 74 Apple MacBook Pro MD313LL/A 1000 73 Dell Inspiron I13Z-6591SLV 700 67 Samsung NP535U3C-A01US 600 63 a. Select a scatter diagram with price as the independent variable. b. What does the scatter diagram developed in part (a) indicate about the relationship…arrow_forward
- To the Internal Revenue Service, the reasonableness of total itemized deductions depends on the taxpayer’s adjusted gross income. Large deductions, which include charity and medical deductions, are more reasonable for taxpayers with large adjusted gross incomes. If a taxpayer claims larger than average itemized deductions for a given level of income, the chances of an IRS audit are increased. Data (in thousands of dollars) on adjusted gross income and the average or reasonable amount of itemized deductions follow. Adjusted Gross Income ($1000s) Reasonable Amount ofItemized Deductions ($1000s) 22 9.6 27 9.6 32 10.1 48 11.1 65 13.5 85 17.7 120 25.5 Compute b1 and b0 (to 4 decimals).b1 b0 Complete the estimated regression equation (to 2 decimals). = + x Predict a reasonable level of total itemized deductions for a taxpayer with an adjusted gross income of $52.5 thousand (to 2 decimals). thousand dollarsWhat is the value, in dollars, of…arrow_forwardK The mean height of women in a country (ages 20-29) is 63.7 inches. A random sample of 65 women in this age group is selected. What is the probability that the mean height for the sample is greater than 64 inches? Assume σ = 2.68. The probability that the mean height for the sample is greater than 64 inches is (Round to four decimal places as needed.)arrow_forwardIn a survey of a group of men, the heights in the 20-29 age group were normally distributed, with a mean of 69.6 inches and a standard deviation of 4.0 inches. A study participant is randomly selected. Complete parts (a) through (d) below. (a) Find the probability that a study participant has a height that is less than 68 inches. The probability that the study participant selected at random is less than 68 inches tall is 0.4. (Round to four decimal places as needed.) 20 2arrow_forward
- PEER REPLY 1: Choose a classmate's Main Post and review their decision making process. 1. Choose a risk level for each of the states of nature (assign a probability value to each). 2. Explain why each risk level is chosen. 3. Which alternative do you believe would be the best based on the maximum EMV? 4. Do you feel determining the expected value with perfect information (EVWPI) is worthwhile in this situation? Why or why not?arrow_forwardQuestions An insurance company's cumulative incurred claims for the last 5 accident years are given in the following table: Development Year Accident Year 0 2018 1 2 3 4 245 267 274 289 292 2019 255 276 288 294 2020 265 283 292 2021 263 278 2022 271 It can be assumed that claims are fully run off after 4 years. The premiums received for each year are: Accident Year Premium 2018 306 2019 312 2020 318 2021 326 2022 330 You do not need to make any allowance for inflation. 1. (a) Calculate the reserve at the end of 2022 using the basic chain ladder method. (b) Calculate the reserve at the end of 2022 using the Bornhuetter-Ferguson method. 2. Comment on the differences in the reserves produced by the methods in Part 1.arrow_forwardYou are provided with data that includes all 50 states of the United States. Your task is to draw a sample of: o 20 States using Random Sampling (2 points: 1 for random number generation; 1 for random sample) o 10 States using Systematic Sampling (4 points: 1 for random numbers generation; 1 for random sample different from the previous answer; 1 for correct K value calculation table; 1 for correct sample drawn by using systematic sampling) (For systematic sampling, do not use the original data directly. Instead, first randomize the data, and then use the randomized dataset to draw your sample. Furthermore, do not use the random list previously generated, instead, generate a new random sample for this part. For more details, please see the snapshot provided at the end.) Upload a Microsoft Excel file with two separate sheets. One sheet provides random sampling while the other provides systematic sampling. Excel snapshots that can help you in organizing columns are provided on the next…arrow_forward
- The population mean and standard deviation are given below. Find the required probability and determine whether the given sample mean would be considered unusual. For a sample of n = 65, find the probability of a sample mean being greater than 225 if μ = 224 and σ = 3.5. For a sample of n = 65, the probability of a sample mean being greater than 225 if μ=224 and σ = 3.5 is 0.0102 (Round to four decimal places as needed.)arrow_forward***Please do not just simply copy and paste the other solution for this problem posted on bartleby as that solution does not have all of the parts completed for this problem. Please answer this I will leave a like on the problem. The data needed to answer this question is given in the following link (file is on view only so if you would like to make a copy to make it easier for yourself feel free to do so) https://docs.google.com/spreadsheets/d/1aV5rsxdNjHnkeTkm5VqHzBXZgW-Ptbs3vqwk0SYiQPo/edit?usp=sharingarrow_forwardThe data needed to answer this question is given in the following link (file is on view only so if you would like to make a copy to make it easier for yourself feel free to do so) https://docs.google.com/spreadsheets/d/1aV5rsxdNjHnkeTkm5VqHzBXZgW-Ptbs3vqwk0SYiQPo/edit?usp=sharingarrow_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





