Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Assignment 5 Employees Average Hours CSCI 251 Problem Statement Suppose the weekly hours for all employees are stored in a text file where each

Lab Assignment 5 Employees Average Hours CSCI 251

Problem Statement

Suppose the weekly hours for all employees are stored in a text file where each line contains the employee name followed by the hours worked for each day of the week. The data is stored as follows (data is continuous in the file but represented in columns below):

John Doe 2 4 3 4 5 8 8

Jane Smith 7 3 4 3 3 4 4

Joe Hart 3 3 4 3 3 2 2

Katie Jones 9 3 4 7 3 4 1

Michael Hu 3 5 4 3 6 3 8

Emily Wright 3 4 4 6 3 4 4

Jim Young 3 7 4 8 3 8 4

Kelly Green 6 3 5 9 2 7 9

Brian Hurley 4 9 8 3 3 2 5

Write a program that reads the contents from the file weeklyHours.txt, and outputs the name of the employee and the average number of hours worked that week.

Input Data

Contents read from weeklyHours.txt

Name followed by 7 numbers (hours worked for the 7 days of the week)

Output Data

Name

Average hours for the week Getting Started

1. On Blackboard, download and save weeklyHours.txt

2. In Command Window type edit hourly.m. You will be prompted that the file does not currently exist; select okay. MATLAB will open the file in the editor.

3. Include header comments formatted as show below (you may want to copy and paste the header from a previous lab assignment and edit it).

% CSCI 251, Section X % Jane Doe % Student ID 12345678 % Lab Assignment 5 % Due % In keeping with the Honor Code of the School of Engineering, I have neither % given nor received assistance from anyone otherthan the instructor. %Problem Statement

Algorithm

1. Clear the Command Window and Workspace windows

2. Write the MATLAB code that will open the file weeklyHours.txt - designate as a read file. In the Command Window type help fopen to learn how to open a file.

3. Use a while-loop to step through the file.Note, the while-loop condition should check for end of file: ~feof(filehandle)

4. Within the while-loop, begin by resetting total to zero (this variable will keep track of the total hours EACH employee has worked).

5. Still within the while-loop, use a for-loop to step through the file, a. Read in oneLine at a time using fgetl(). Note that the variable j is used as an example variable to step through and populate the employeeHours array. You do not have to use j. b. Since each employee has hours listed for all 7 days, you may hard code this into your program by using a for-loop that counts from 1 to 8 (for i = 1: 1: 8). c. When you are at count 1 (i = 1), this is the employees name and should be added to your array employeeHours{ j }; Use an if-statement to check for the count. Increment j so you dont overwrite the elements in your array. d. When you are at counts 2 through 8, convert the string to a number (str2num), add each number to the total (you are adding up all of the hours for the week for THIS employee). e. Continue to read in one line from your file (still within the for-loop

6. Once your count has reached 8 (i.e., i ), exit your for-loop and calculate the average by dividing the total by 7. Add this value to your array employeeHours{ j }. You will need to increment j again.

7. Because the file can be separated into 8-row blocks (like the data listed on page 1) each employee requires 8 rows of data (one row for their name and seven rows for their hours worked).

8. Repeat steps 4-7 until you are at the end of the file (the while-loop controls this).

9. Once youve finished reading from the file (exited while-loop) use another for-loop to step through the employeeHours array to display the name of the employee and the average hours he/she worked. Refer to the sample output above on how your results should be displayed to the monitor. Your employeeHours array should be populated as follows:

employeeHours John Doe 4.86 Jane Smith 4 Joe Hart 2.86 Katie Jones 4.43 Michael Hu 4.57 Emily Wright 4 Jim Young 5.29 Kelly Green 5.86 Brian Hurley 4.86

Sample Output:

John Doe average hours worked: 4.86

Jane Smith average hours worked: 4.00

Joe Hart average hours worked: 2.86

Katie Jones average hours worked: 4.43

Michael Hu average hours worked: 4.57

Emily Wright average hours worked: 4.00

Jim Young average hours worked: 5.29

Kelly Green average hours worked: 5.86

Brian Hurley average hours worked: 4.86

10.Before each significant step, provide a comment explaining the step (do not comment every line of code).

Test Plan

In order to receive credit, you must show the TA the test case using weeklyHours.txt. Also, submit your lab assignment on Blackboard under Lab Assignments. Your electronic submission of the program file will represent your endorsement of the Honor Code Statement.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

(-1)". n = 1 (x+5)" 3"n(n+1)

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago