
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
- . This problem will yield a standard formula given in elementary statistics for a least squares line, making use of the normal equations. (a) Given pairs of data points (x1, Y1), (x2, Y2), ..., (xn, Yn) consider approximating lines of the form y = mx+b. The error e; for the ith pair is the distance between y; and the height (y value) of the line at xi. This is ei = Yi — (mxi + b). If we consider the equations b + x;m = Yi for i n in the variables b and m we can = 1,2, = think of this as a system of equations Ax = 6 where A 1 x1 x2 = : [m] Хп Y1 Уп numbers. Here, note that the variables are m and b and the xi, Yi are given The least squares approximation for this system (which gives the intercept b and slope m of the best least squares line for the data) is the solution to the normal equations AT Ax = ATb. Determine ATA (a 2×2 matrix) and AT (a 2×1 matrix). The entries will be sums of terms involving the x; and y₁. Write these, first using Σ notation and then simplify the notation using…arrow_forward= a) Recall that the formula for the projection p of vector & onto vector a is p = ±ªã. The plane through the origin in R³ given by ax+by+cz = 0 for real numbers a, b, c, is the set of all points (viewed as vectors) orthogonal to the normal n = (a, b, c) to the plane. To find the distance from a point w (xo, Yo, zo) to the plane ax+by+cz = 0 we can find the projection of w onto the normal ñ and then find the length of this projection. Do this to derive a generic formula for the distance in terms of xo, Yo, Zo, a, b, c. First find the length squared and then take a root. b) To find the distance from a point w (xo, Yo, zo) to a plane ax + by + cz = d, not necessarily not through the origin, one approach is to shift all z coordinates down by d. That is, replace (x, y, z) by (x, y, z — d). The relationship between the plane and w is unchanged so we can find the distance by finding the distance between (xo, Yo, zo - d) and the plane ax + by+cz = 0 through the origin. Do this to derive a…arrow_forwardUse Gram-Schmidt orthogonalization to find an orthonormal set of vectors with the same span as = 1 2 -0-08-0 a2 = 3 a3 = -2 Find also a QR factorization A = QR with the columns of Q the orthonormal set found above and R upper triangular and invertible and A with columns ā₁, ā2, ā3.arrow_forward
- (a) Let Q be an orthonormal matrix. That is, the columns of Q have unit length and are pairwise orthogonal. Prove that the length of Qã is equal to the length of for any column vector x. Note that you can do this by showing that the lengths squared are equal. (b) Prove that if A and B are symmetric and πT Ax = x² Bã for all & then A = B. Make use of coordinate vectors ē;, which are 0 except for a 1 in position i. Explain why, for matrix A, entries can be found by arj = Aej. First show that diagonal = = entries aii bir. Then consider what (ē¿ + ē¿)ªA(ē¿ + ē¿) in terms of entries of A (and similarly for B) and make use of symmetry aij = aji and similarly for B. (c) Prove the converse: If for all x, ||Q|| ||||| then Q is orthonormal. One ap- proach is to use part (b) to show that QTQ = I. Recall that QTQ is symmetric (and trivially I is symmetric). Recall also that, written in matrix notation, vector length (squared) is given by ||7||2 = (this is the dot product written in matrix…arrow_forward(a) What is wrong with the following statement and proof? A is invertible if and only if ATA is invertible. 'Proof': Since det(A) = det(AT), det(ATA) = det(A)². Either both are 0 (and both A and ATA are not invertible) or both are nonzero (and both A and ATA are invertible). (b) Prove that Null(A) C Null(ATA). That is, prove that if Ax = 0 then AT Ax = 0. (c) Prove that Null(ATA) C Null(A). That is, prove that if AT Añ = 0 then Aã = Ổ. Recall that the length (squared) of a vector v is V, which written in matrix multiplication notation is Tv. (d) Parts (b) and (c) imply that Null(A) = • Nul(ATA). Use this to prove that the columns of A are linearly independent if and only if ATA has an inverse.arrow_forwardGive a 99% confidence interval, for μ1 M2 given the following information. n₁ = 20, 1 = 2.31, s₁ = 0.47 n2 = 40, 2 = 2.21, 82 = 0.55 + Use Technology Rounded to 2 decimal places. Hint Question Help: Video Submit Question ch Η UP T SINK O DELL F2 F3 F4 F5 F6 F7 F8 144 ►11 4arrow_forward
- Please could you provide solutions to the following questions. Thanksarrow_forwardPlease could you provide solutions to the following questions. Thanksarrow_forwardor Beer and blood alcohol content: Many people believe that gender, weight, drinking habits, and many other factors are much more important in predicting blood alcohol content (BAC) than simply considering the number of drinks a person consumed. Here we examine data from sixteen student volunteers at Ohio State University who each drank a randomly assigned number of cans of beer. These students were evenly divided between men and women, and they differed in weight and drinking habits. Thirty minutes later, a police officer measured their blood alcohol content (BAC) in grams of alcohol per deciliter of blood (Malkevitc and Lesser, 2008). The scatterplot and regression table summarize the findings. to search BAC (grams per deciliter) 0.15- 0.10- 0.05- Part of Ti Calculator Output Ti Calculator y = ax+b Be and p = 0 t = 7.48 P 0.0000 df = 14 a = 0.0180 b = -0.0127 Part of Excel Output 2 4 0 0 6 Cans of beer (Intercept) beers Estimate -0.0127 0.0180 Std. Error t value 0.0126 P(>|t|) -1.00…arrow_forward
- Proctor ts Ces rces arch Question 31 The data below was collected from manufacturer advertisements of their vehicles horsepower (x) and highway gas mileage (mpg=y). Use this data to answer the following questions. horsepower 157 250 340 350 390 190 220 mpg 33 28 15 17 11 35 42 1. Find the p-value to determine if there is a linear correlation between horsepower and highway gas mileage (mpg). Record the p-value below. Round to four decimal places. p-value= 2. Is there a linear correlation between horsepower and highway gas mileage (mpg)? ? 3. If there is a linear correlation, write the correlation coefficient below. Otherwise, leave it blank. Round your final answer to four decimal places. Be careful with your sign. T= 4. If there is a linear correlation, write the regression equation below. Otherwise, leave it blank. Round all numbers to four decimal places. ŷ 5. Using the data shown above, predict the the highway gas mileage (mpg) for a car that has a horsepower of 225. Round your…arrow_forwardarch es For Question 24 < Annual high temperatures in a certain location have been tracked for several years. Let X represent the number of years after 2000 and Y the high temperature. Based on the data shown below, calculate the linear regression equation using technology (each number to three decimal places). X y 4 33.38 5 31 6 31.92 7 33.64 8 33.06 9 32.28 10 33.7 11 30.82 12 31.14 13 32.56 14 34.58 Click Download CSV to download csv file of data or copy/paste the data into Excel. After downloading the file, you may want to save it as an Excel Workbook. The fitted regression equation is ŷ = Interpret the y-intercept of the equation: O In 2000, the temperature was about 32.152. In 2014, the temperature was about 34.58. In 2004, the temperature was about 32.152. In 2004, the temperature was about 0.045. Question Help: Video Submit Question F2 F3 F4 144 UP T M F5 114 % L F6 144 DOLL F7 6 F8 F9 F10 &arrow_forwardMath 105Z Worksheet Week 8 Instructions: Answer questions about the following contexts. You'll be graded for accuracy on problems 3 and 4 but you should try to complete the entire worksheet for full credit (see the rubric on Canvas). You'll need to upload your work as a single PDF to the appropriate assignment in Canvas. 1. A random sampling of the speeds of 12 cars monitored on a small stretch of highway yielded the following data: 45 55 50 60 60 55 60 50 70 65 55 60 (a) Find the mean speed. (b) Find the median speed. (c) Find the mode speed. (d) Construct a histogram to represent the data. 2. The mean exam score for the 24 students in section M is 72, and the mean exam score for the 30 students of section N is 78. Find the mean score for all 54 students. Page 1arrow_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





