
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 33E
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: A Practical Introduction to Programming and Problem Solving
- Question 5 10 pts 1 Details A college administrator claims that 80% of college students purchase their books from the campus bookstore. You this is inaccurate and form a random sample of 57 students at that college and find that 36 of them purchased their books from the bookstore. Test the administrator's claim using a level of significance of 1%. a. What type of test will be used in this problem? A test for a proportion b. What evidence justifies the use of this test? Check all that apply np5 and nq > 5 The population standard deviation is not known The original population is approximately normal There are two different samples being compared The sample size is larger than 30 The sample standard deviation is not known The population standard deviation is known c. Enter the null hypothesis for this test. Ho p 0.80 d. Enter the alternative hypothesis for this test. H₁p 0.80 e. Is the original claim located in the null or alternative hypothesis? Null Hypothesis f. What is the test…arrow_forwardAre there less children diagnosed with Autism Spectrum Disorder (ASD) in states that have larger urban areas over states that are mostly rural? Assume data were collected from a fairly urban state and they found 195 eight-year olds diagnosed with AGD out of 18183 eight-year olds evaluated. Assume data were then collected for a fairly rural state and they found 54 eight-year olds diagnosed with AGD out of 2179 eight-year olds evaluated. is there enough evidence to show that the proportion of children diagnosed with ASD in the fairly urban state is lower than the proportion in the fairly rural state? a.) Test at the 3% level b.) Compute a 97% confidence interval for the difference in proportions. Use the following steps for the hypothesis test. For the confidence interval you do not need to do all the steps since you did some of them already in hypothesis test. Part a.) HYPOTHESIS TEST Parameter What is the correct parameter symbol and wording for population 1? P The proportion of all…arrow_forwardIn 1912 the luxury liner Titanic struck an iceberg and sank on its first voyage. Some passengers got off the ship in lifeboats, but many died. Here is information about who lived and who died, by gender. Men Women Total Died 680 126 806 Survived 168 317 485 Total 848 443 1291 Now look at the data set comparing gender and survival. 1. Suggest two tests you can do to see if there is a relationship. 2. For each test, clearly formulate the null and alternative hypothesis. Make sure to define any parameters you used when setting up the null and alternative hypothesis.arrow_forward
- When there are two independent samples to compare two population means, if we don't know that the population variances are equal, then it does not matter because there are paired samples. do not assume population variances are equal. assume population variances are equal. none of these.arrow_forwardDoes it take less time for seeds to germinate if they are near rock music that is continuously playing compared to being near classical music? The time (in days) to germinate was recorded for 30 seeds that were randomly exposed to rock music took and 26 seeds that were randomly exposed to classical music. a. Based on the given information, select ALL that are true about these data. There are two independent random samples of numerical data. Samples are not random. There are two paired random samples of numerical data. Each sample size is large enough. Each population has a normal distribution. At least one sample size is not large enough. Data are not numerical. Each sample data have approximately a normal distribution with no outliers. b. Are all the assumptions met for a two-sample t-test? Yes O No > Next Question to search UP T M365 F2 F3 F4 <400 2 144 64 $ DELL F5 F6 F7 F8 ►11 25 %arrow_forward4 Cor The following data show the results of two random samples that measured the average number of minutes per charge for AA Lithium-ion (Li-ion) rechargeable batteries versus Nickel-Metal Hydride (NiMH) rechargeable batteries. Click this button to reveal the data. Click again to collapse it. Li-ion NiMH 107 86 95 91 87 89 84 88 89 88 98 87 95 86 85 90 84 91 96 85 97 87 100 89 86 91 102 89 87 94 78 arch If you want to do well, please avoid using Google sheets. Perform a hypothesis test using significance level (a) = .02 to determine if the true average number of minutes per charge for NiMH batteries is different from that for Li-ion batteries. a. Perform a two-sample t test to test the claim that the true average number of minutes per charge for Li-ion batteries is different from that for NiMH batteries. Fill in the blanks for the alternative hypothesis of this test. Select an answer Select an answer Based on your work, answer the following questions. The observed difference in mean…arrow_forward
- mts ces urces A group of economists wants to compare the mean annual leave among the US and EU workers. Two samples of US and EU workers were obtained independently and analyzed. The sample of 45 US workers had the mean annual leave of 18.8 days and the standard deviation 9.05 days. The sample of 31 EU workers had the mean annual leave of 25.6 days and the standard deviation 5.38 days. Use 4% level of significance to decide whether there is sufficient evidence that mean annual leave of US workers is less than mean annual leave of EU workers. Procedure: Two means T Hypothesis Test Assumptions: (select everything that applies) Normal populations The number of positive and negative responses are both greater than 10 for both samples Population standard deviations are known Simple random samples Population standard deviation are unknown and not assumed equal Sample sizes are both greater than 30 Independent samples Paired samples Population standard deviation are unknown but assumed equal…arrow_forwardHow do the assumptions and characteristics of the linear regression model influence the reliability and robustness of inference drawn from its results in diverse research scenarios?arrow_forwardA group of economists wants to compare the mean annual leave among the US and EU workers. Two samples of US and EU workers were obtained independently and analyzed. The sample of 45 US workers had the mean annual leave of 18.8 days and the standard dediation 9.05 days. The sample of 31 EU workers had the mean annual leave of 25.6 days and the standard deviation 5.38 days. Use 4% level of significance to decide whether there is sufficient evidence that mean annual leave of US workers is less than mean annual leave of EU workers. Procedure: Select an answer Assumptions: (select everything that applies) Normal populations The number of positive and negative responses are both greater than 10 for both samples Population standard deviations are known Simple random samples Population standard deviation are unknown and not assumed equal Sample sizes are both greater than 30 Independent samples Paired samples Population standard deviation are unknown but assumed equal Part 1: Hypothesis Test..…arrow_forward
- A group of economists wants to compare the average annual leave among the US and EU workers. Two samples of US and EU workers were obtained independently and analyzed. The sample of 37 US workers had the average annual leave of 18 days and the standard deviation 9.07 days. The sample of 40 EU workers had the average annual leave of 26.8 days and the standard deviation 5.11 days. Use 10% level of significance to decide whether there is sufficient evidence that average annual leave of US workers is less than average annual leave of EU workers. Procedure: Two means T Hypothesis Test Assumptions: (select everything that applies) Population standard deviations are known Population standard deviation are unknown but assumed equal Independent samples Paired samples The number of positive and negative responses are both greater than 10 for both samples Sample sizes are both greater than 30 Population standard deviation are unknown and not assumed equal Normal populations Simple random samples…arrow_forwardLEAVE! A group of economists wants to compare the average annual leave among the US and EU workers. Two samples of US and EU workers were obtained independently and analyzed. The sample of 37 US workers had the average annual leave of 18 days and the standard deviation 9.07 days. The sample of 40 EU workers had the average annual leave of 26.8 days and the standard deviation 5.11 days. Use 10% level of significance to decide whether there is sufficient evidence that average annual leave of US workers is less than average annual leave of EU workers. Procedure: Two means T Hypothesis Test Assumptions: (select everything that applies) Population standard deviations are known Population standard deviation are unknown but assumed equal Independent samples Paired samples D The number of positive and negative responses are both greater than 10 for both samples Sample sizes are both greater than 30 Population standard deviation are unknown and not assumed equal Normal populations Simple random…arrow_forwardFall 2025 Home C Canvas - Homework 6 Secure Exam Proctor Proctorio) Announcements Modules A group of economists wants to compare the mean annual leave among the US and EU workers. Two samples of US and EU workers were obtained independently and analyzed. The sample of 34 US workers had the mean annual leave of 14.7 days and the standard deviation 7.95 days. The sample of 32 EU workers had the mean annual leave of 23.5 days and the standard deviation 3.86 days. Use 10% level of significance to decide whether there is sufficient evidence that mean annual leave of US workers is less than mean annual leave of EU workers. Procedure: Two means T Hypothesis Test くくる Discussions eople Grades enji brary Resources ampus Resources Assumptions: (select everything that applies) Independent samples simple random samples Population standard deviations are known Normal populations The number of positive and negative responses are both greater than 10 for both samples Population standard deviation are…arrow_forward
Glencoe Algebra 1, Student Edition, 9780079039897...AlgebraISBN:9780079039897Author:CarterPublisher:McGraw Hill
Holt Mcdougal Larson Pre-algebra: Student Edition...AlgebraISBN:9780547587776Author:HOLT MCDOUGALPublisher:HOLT MCDOUGAL
Big 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 Learning
College Algebra (MindTap Course List)AlgebraISBN:9781305652231Author:R. David Gustafson, Jeff HughesPublisher:Cengage Learning





