
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
- If a uniform distribution is defined over the interval from 6 to 10, then answer the followings: What is the mean of this uniform distribution? Show that the probability of any value between 6 and 10 is equal to 1.0 Find the probability of a value more than 7. Find the probability of a value between 7 and 9. The closing price of Schnur Sporting Goods Inc. common stock is uniformly distributed between $20 and $30 per share. What is the probability that the stock price will be: More than $27? Less than or equal to $24? The April rainfall in Flagstaff, Arizona, follows a uniform distribution between 0.5 and 3.00 inches. What is the mean amount of rainfall for the month? What is the probability of less than an inch of rain for the month? What is the probability of exactly 1.00 inch of rain? What is the probability of more than 1.50 inches of rain for the month? The best way to solve this problem is begin by a step by step creating a chart. Clearly mark the range, identifying the…arrow_forwardClient 1 Weight before diet (pounds) Weight after diet (pounds) 128 120 2 131 123 3 140 141 4 178 170 5 121 118 6 136 136 7 118 121 8 136 127arrow_forwardClient 1 Weight before diet (pounds) Weight after diet (pounds) 128 120 2 131 123 3 140 141 4 178 170 5 121 118 6 136 136 7 118 121 8 136 127 a) Determine the mean change in patient weight from before to after the diet (after – before). What is the 95% confidence interval of this mean difference?arrow_forward
- In order to find probability, you can use this formula in Microsoft Excel: The best way to understand and solve these problems is by first drawing a bell curve and marking key points such as x, the mean, and the areas of interest. Once marked on the bell curve, figure out what calculations are needed to find the area of interest. =NORM.DIST(x, Mean, Standard Dev., TRUE). When the question mentions “greater than” you may have to subtract your answer from 1. When the question mentions “between (two values)”, you need to do separate calculation for both values and then subtract their results to get the answer. 1. Compute the probability of a value between 44.0 and 55.0. (The question requires finding probability value between 44 and 55. Solve it in 3 steps. In the first step, use the above formula and x = 44, calculate probability value. In the second step repeat the first step with the only difference that x=55. In the third step, subtract the answer of the first part from the…arrow_forwardIf a uniform distribution is defined over the interval from 6 to 10, then answer the followings: What is the mean of this uniform distribution? Show that the probability of any value between 6 and 10 is equal to 1.0 Find the probability of a value more than 7. Find the probability of a value between 7 and 9. The closing price of Schnur Sporting Goods Inc. common stock is uniformly distributed between $20 and $30 per share. What is the probability that the stock price will be: More than $27? Less than or equal to $24? The April rainfall in Flagstaff, Arizona, follows a uniform distribution between 0.5 and 3.00 inches. What is the mean amount of rainfall for the month? What is the probability of less than an inch of rain for the month? What is the probability of exactly 1.00 inch of rain? What is the probability of more than 1.50 inches of rain for the month? The best way to solve this problem is begin by creating a chart. Clearly mark the range, identifying the lower and upper…arrow_forwardProblem 1: The mean hourly pay of an American Airlines flight attendant is normally distributed with a mean of 40 per hour and a standard deviation of 3.00 per hour. What is the probability that the hourly pay of a randomly selected flight attendant is: Between the mean and $45 per hour? More than $45 per hour? Less than $32 per hour? Problem 2: The mean of a normal probability distribution is 400 pounds. The standard deviation is 10 pounds. What is the area between 415 pounds and the mean of 400 pounds? What is the area between the mean and 395 pounds? What is the probability of randomly selecting a value less than 395 pounds? Problem 3: In New York State, the mean salary for high school teachers in 2022 was 81,410 with a standard deviation of 9,500. Only Alaska’s mean salary was higher. Assume New York’s state salaries follow a normal distribution. What percent of New York State high school teachers earn between 70,000 and 75,000? What percent of New York State high school…arrow_forward
- Pls help asaparrow_forwardSolve the following LP problem using the Extreme Point Theorem: Subject to: Maximize Z-6+4y 2+y≤8 2x + y ≤10 2,y20 Solve it using the graphical method. Guidelines for preparation for the teacher's questions: Understand the basics of Linear Programming (LP) 1. Know how to formulate an LP model. 2. Be able to identify decision variables, objective functions, and constraints. Be comfortable with graphical solutions 3. Know how to plot feasible regions and find extreme points. 4. Understand how constraints affect the solution space. Understand the Extreme Point Theorem 5. Know why solutions always occur at extreme points. 6. Be able to explain how optimization changes with different constraints. Think about real-world implications 7. Consider how removing or modifying constraints affects the solution. 8. Be prepared to explain why LP problems are used in business, economics, and operations research.arrow_forwardged the variance for group 1) Different groups of male stalk-eyed flies were raised on different diets: a high nutrient corn diet vs. a low nutrient cotton wool diet. Investigators wanted to see if diet quality influenced eye-stalk length. They obtained the following data: d Diet Sample Mean Eye-stalk Length Variance in Eye-stalk d size, n (mm) Length (mm²) Corn (group 1) 21 2.05 0.0558 Cotton (group 2) 24 1.54 0.0812 =205-1.54-05T a) Construct a 95% confidence interval for the difference in mean eye-stalk length between the two diets (e.g., use group 1 - group 2).arrow_forward
- An article in Business Week discussed the large spread between the federal funds rate and the average credit card rate. The table below is a frequency distribution of the credit card rate charged by the top 100 issuers. Credit Card Rates Credit Card Rate Frequency 18% -23% 19 17% -17.9% 16 16% -16.9% 31 15% -15.9% 26 14% -14.9% Copy Data 8 Step 1 of 2: Calculate the average credit card rate charged by the top 100 issuers based on the frequency distribution. Round your answer to two decimal places.arrow_forwardPlease could you check my answersarrow_forwardLet Y₁, Y2,, Yy be random variables from an Exponential distribution with unknown mean 0. Let Ô be the maximum likelihood estimates for 0. The probability density function of y; is given by P(Yi; 0) = 0, yi≥ 0. The maximum likelihood estimate is given as follows: Select one: = n Σ19 1 Σ19 n-1 Σ19: n² Σ1arrow_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





