How do I make my output on this code look like the one on the picture Employees
ID: 3854069 • Letter: H
Question
How do I make my output on this code look like the one on the picture Employees whose payroll is larger than 483.80 (Ave)#include <iostream> #include <cstring> #include <fstream> #include <iomanip>
using namespace std;
struct employeeType { string name; double hours; double hourlyPay; double amountEarned; };
string format(string str); void readData(employeeType emp[], int& counter, ifstream& fileContent); void bubblesort(employeeType emp[], int count); void weeklyPay(employeeType arr[], int MAX); void printEmployeesAboveAvg(employeeType arr[], int MAX); void printEmployeeData(employeeType arr[], int MAX);
string format(string str) { for (int i = 0; i < str.length(); i++) { if (i == 0) str[i] = toupper(str[i]); else str[i] = tolower(str[i]); } return str; }
void readData(employeeType emp[], int& counter, ifstream& fileContent) { int i = 0; while (fileContent >> emp[i].name >> emp[i].hours >> emp[i].hourlyPay) { emp[i].name = format(emp[i].name); i++; } }
void bubblesort(employeeType arr[], int MAX) { int n = MAX; // Sorting strings using bubble sort bool didSwap; employeeType temp;
do { didSwap = false; //assume there are no swaps
for (int i = 0; i < n - 1; i++) { if (arr[i].name > arr[i + 1].name) { //I am not cheating temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; didSwap = true; //there was a swap... } } } while (didSwap); }
void weeklyPay(employeeType emp[], int MAX) { for (int i = 0; i < MAX; i++) { if (emp[i].hours > 40) { emp[i].amountEarned = emp[i].hourlyPay * emp[i].hours * (1.5); } else { emp[i].amountEarned = emp[i].hourlyPay * emp[i].hours; } } } void printEmployeesAboveAvg(employeeType emp[], int MAX) {
double temp = 0; for (int i = 0; i < MAX; i++) { if(emp[i].name != "") temp = temp + emp[i].amountEarned; } double avarage = temp / MAX; cout << " Employees whose payroll is larger than " << avarage << " (Ave)" << endl; for (int i = 0; i < MAX; i++) { if (emp[i].amountEarned > avarage) if(emp[i].name != "") cout << emp[i].name << endl; } }
void printEmployeeData(employeeType emp[], int MAX) { cout << " " << left << setw(30) << " Employee Name " << left << setw(30) << " Hours worked " << left << setw(30) << " Hourly wage " << left << setw(30) << " Amount earned " << endl;
for (int i = 0; i < MAX; i++) { if (emp[i].name != "") //setw means set width between columns according to the their sizes cout << "" << left << setw(36) << emp[i].name << right << setw(8) << emp[i].hours << right << setw(29) << emp[i].hourlyPay << right << setw(32) << emp[i].amountEarned << endl; } }
int main() { ifstream inFile; struct employeeType emp[200]; char fileName[250]; cout << "Enter inputfile: "; cin >> fileName; inFile.open(fileName); int i=0; readData(emp, i, inFile); i = sizeof(emp) / sizeof(emp[0]); bubblesort(emp, i); weeklyPay(emp, i);
cout << fixed << setprecision(2); printEmployeeData(emp, i); printEmployeesAboveAvg(emp, i);
} How do I make my output on this code look like the one on the picture Employees whose payroll is larger than 483.80 (Ave)
#include <iostream> #include <cstring> #include <fstream> #include <iomanip>
using namespace std;
struct employeeType { string name; double hours; double hourlyPay; double amountEarned; };
string format(string str); void readData(employeeType emp[], int& counter, ifstream& fileContent); void bubblesort(employeeType emp[], int count); void weeklyPay(employeeType arr[], int MAX); void printEmployeesAboveAvg(employeeType arr[], int MAX); void printEmployeeData(employeeType arr[], int MAX);
string format(string str) { for (int i = 0; i < str.length(); i++) { if (i == 0) str[i] = toupper(str[i]); else str[i] = tolower(str[i]); } return str; }
void readData(employeeType emp[], int& counter, ifstream& fileContent) { int i = 0; while (fileContent >> emp[i].name >> emp[i].hours >> emp[i].hourlyPay) { emp[i].name = format(emp[i].name); i++; } }
void bubblesort(employeeType arr[], int MAX) { int n = MAX; // Sorting strings using bubble sort bool didSwap; employeeType temp;
do { didSwap = false; //assume there are no swaps
for (int i = 0; i < n - 1; i++) { if (arr[i].name > arr[i + 1].name) { //I am not cheating temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; didSwap = true; //there was a swap... } } } while (didSwap); }
void weeklyPay(employeeType emp[], int MAX) { for (int i = 0; i < MAX; i++) { if (emp[i].hours > 40) { emp[i].amountEarned = emp[i].hourlyPay * emp[i].hours * (1.5); } else { emp[i].amountEarned = emp[i].hourlyPay * emp[i].hours; } } } void printEmployeesAboveAvg(employeeType emp[], int MAX) {
double temp = 0; for (int i = 0; i < MAX; i++) { if(emp[i].name != "") temp = temp + emp[i].amountEarned; } double avarage = temp / MAX; cout << " Employees whose payroll is larger than " << avarage << " (Ave)" << endl; for (int i = 0; i < MAX; i++) { if (emp[i].amountEarned > avarage) if(emp[i].name != "") cout << emp[i].name << endl; } }
void printEmployeeData(employeeType emp[], int MAX) { cout << " " << left << setw(30) << " Employee Name " << left << setw(30) << " Hours worked " << left << setw(30) << " Hourly wage " << left << setw(30) << " Amount earned " << endl;
for (int i = 0; i < MAX; i++) { if (emp[i].name != "") //setw means set width between columns according to the their sizes cout << "" << left << setw(36) << emp[i].name << right << setw(8) << emp[i].hours << right << setw(29) << emp[i].hourlyPay << right << setw(32) << emp[i].amountEarned << endl; } }
int main() { ifstream inFile; struct employeeType emp[200]; char fileName[250]; cout << "Enter inputfile: "; cin >> fileName; inFile.open(fileName); int i=0; readData(emp, i, inFile); i = sizeof(emp) / sizeof(emp[0]); bubblesort(emp, i); weeklyPay(emp, i);
cout << fixed << setprecision(2); printEmployeeData(emp, i); printEmployeesAboveAvg(emp, i);
}
Description A company is planning to hire a number of temporary workers who are paid hourly and you are given a data file that contains the last name of the employees, the amount of hours worked by the employee for the week, and their hourly wage. You are asked to write a program that computes each employee's weekly pay and the average salary of all the workers. The program then outputs the weekly pay of each employee, the average weekly pay, and the names of all the employees whose pay is greater than or equal to the average pay (Sorted by the last name, you will also need to format the name so the first letter is capital and the rest are lower case, using the format function). If the number of hours worked in a week is more than 40 hours, then the pay rate for the hours over 40 is 1.5 times the regular hourly rate for each hour above 40. In this assignment, instead of using parallel arrays, you will use an array of structs to hold the employees' names, hours worked, hourly pay, and amount earned, the definition of the struct can be seen below struct employeeType string name; double hours; double hourlyPay; double amountEarned; Where string name holds the name of the employee, double hours wl hold the hours the employee has worked, double hourlyPay wl hold the hourly pay for the employee, and double amountEarned wi be the total weekly wage. So unlike the last assignment, you won't have two parallel arrays, you wl have an array of structs and each element of the array will hold all the information for each employee. You will write the following functions string format (string) this function wl format the string passed as a parameter and return this string in the format where the first letter is upper cased and the rest of the characters wil be lower cased void readData (employeeType[], int&, ifstream&) this function takes in the struct array of the employee's info, the array counter, and the input filestream, you will populate the struct array with the elds of each struct and you will increment the array counter contents of the file into the appropriate f after each line has been read void bubblesort (employeeType[], int) this function takes the struct array with its counter and will sort the array by name
Explanation / Answer
/* Program ARRAYMAX.C ** ** Illustrates function to find the max all of the elements of an array ** ** Peter H. Anderson, MSU, Feb 21, '97 ** */ #include int max_array(int a[], int num_elements); void print_array(int a[], int num_elements); void main(void) { int a[10] = {1, 2, 0, 0, 4, 5, 6, 9, 9, 17}; int max; printf(" Array: "); print_array(a, 10); max = max_array(a, 10); printf("The max is %d ", max); } int max_array(int a[], int num_elements) { int i, max=-32000; for (i=0; imax) { max=a[i]; } } return(max); } void print_array(int a[], int num_elements) { int i; for(i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.