MATLAB: A Practical Introduction to Programming and Problem Solving
MATLAB: A Practical Introduction to Programming and Problem Solving
5th Edition
ISBN: 9780128154793
Author: Stormy Attaway Ph.D. Boston University
Publisher: Elsevier Science
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 9, Problem 33E
Expert Solution & Answer
Check Mark
To determine

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 file

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:

MATLAB: A Practical Introduction to Programming and Problem Solving, Chapter 9, Problem 33E , additional homework tip  1

MATLAB: A Practical Introduction to Programming and Problem Solving, Chapter 9, Problem 33E , additional homework tip  2

Therefore, the script is saved and executed.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
or Testing a claim about a population mean: t-Test An overly involved "neighborhood watch" group has been investigating the length of lawns. In years past, lawns had been mowed to a mean length of 2.4 inches. A recent random sample of lawn lengths is given below. Conduct an appropriate test, at a 5% significance level, to determine if the overall mean lawn length is now higher than 2.4 inches. Data (lawn lengths, measured in inches): 4.35 3.73 3.35 2.75 2.96 1.97 2.02 3.68 3.61 3.87 3.92 4.50 3.66 3.60 3.01 4.34 4.23 4.95 2.97 Checksum: 67.47 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. A. Consider the parameter of interest and choose the correct alternative hypothesis. Ομ = 2.4 Ο μ > 2.4 Op 2.4 Ομ < 2.4 Ομ Σ 2.4 Ομ 5 2.4 B. Are the necessary conditions present to carry out this inference procedure? Explain in context. Yes. Our sample was not larger than 30 in size, but the…
Proctor ents ources Sources button below for formula and a similar problem. A researcher believes a new diet will have a positive impact on the health of adult lab mice but the effects on the weight of the mice are unknown. A sample of 10 mice was obtained and the mice were weighed before and after the diet treatment. Listed below are the measurement results: Click this button to reveal the data. Click again to collapse it. Mouse Before(g) After(g) Difference(g) 1 37.8 37.5 0.3 2 38.2 37.6 0.6 3 36.6 35.2 1.4 4 36.6 35.4 1.2 5 37.6 37.1 0.5 6 38.1 37.3 0.8 7 36.4 35 1.4 8 35.9 34.6 1.3 9 35.3 34.9 0.4 10 40.1 39.2 0.9 Click this button to reveal the summary of data. Click again to collapse it. Average Standard deviation Before(g) After(g) Difference(g) 37.26 1.39 36.38 1.55 0.88 0.42 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. Use 5% level of significance to decide whether…
Does 10K running time increase when the runner listens to music? Nine runners were timed as they ran a 10K with and without listening to music. The running times in minutes are shown below. With Music Without Music Running Time 57 40 39 60 58 47 50 42 45 58 36 39 51 56 50 52 41 41 Assume a Normal distribution. What can be concluded at the the a 0.10 level of significance? For this study, we should use t-test for the difference between two dependent population means o search a. The null and alternative hypotheses would be: Houd 0 (please enter a decimal) H₁ ud 0 (Please enter a decimal) b. The test statistic = (please show your answer to 3 decimal places.) c. The p-value (Please show your answer to 4 decimal places.) d. The p-value is >v a e. Based on this, we should fail to reject f. Thus, the final conclusion is that ... the null hypothesis. The results are statistically significant at a = 0.10, so there is sufficient evidence to conclude M365 UP T DOLL
Knowledge Booster
Background pattern image
Statistics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, statistics and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Glencoe Algebra 1, Student Edition, 9780079039897...
Algebra
ISBN:9780079039897
Author:Carter
Publisher:McGraw Hill
Text book image
Holt Mcdougal Larson Pre-algebra: Student Edition...
Algebra
ISBN:9780547587776
Author:HOLT MCDOUGAL
Publisher:HOLT MCDOUGAL
Text book image
Big Ideas Math A Bridge To Success Algebra 1: Stu...
Algebra
ISBN:9781680331141
Author:HOUGHTON MIFFLIN HARCOURT
Publisher:Houghton Mifflin Harcourt
Text book image
Elementary Geometry For College Students, 7e
Geometry
ISBN:9781337614085
Author:Alexander, Daniel C.; Koeberlein, Geralyn M.
Publisher:Cengage,
Text book image
Functions and Change: A Modeling Approach to Coll...
Algebra
ISBN:9781337111348
Author:Bruce Crauder, Benny Evans, Alan Noell
Publisher:Cengage Learning
Text book image
College Algebra (MindTap Course List)
Algebra
ISBN:9781305652231
Author:R. David Gustafson, Jeff Hughes
Publisher:Cengage Learning
Sampling Methods and Bias with Surveys: Crash Course Statistics #10; Author: CrashCourse;https://www.youtube.com/watch?v=Rf-fIpB4D50;License: Standard YouTube License, CC-BY
Statistics: Sampling Methods; Author: Mathispower4u;https://www.youtube.com/watch?v=s6ApdTvgvOs;License: Standard YouTube License, CC-BY