How do I make my output on this code look like the one on the picture Employees
ID: 3854086 • 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). Can you modify it so I can get this output#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). Can you modify it so I can get this output
#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
I don't seem to understand how Integer.MAX_VALUE and Integer.MIN_VALUE help in finding the min and max value in an array.
I understand how this method (pseudocode below) works when finding the min and max values:
But as for this method, I don't understand the purpose of Integer.MAX_VALUE and Integer.MIN_VALUE:
I don't seem to understand how Integer.MAX_VALUE and Integer.MIN_VALUE help in finding the min and max value in an array.
I understand how this method (pseudocode below) works when finding the min and max values:
max = A[0], min = A[0] for each i in A if A[i] > max then max = A[i] if A[i] < min then min = A[i] But as for this method, I don't understand the purpose of Integer.MAX_VALUE and Integer.MIN_VALUE:
import java.util.Scanner; class MyClass { public static void main(String[] args) { int[] numbers; // declaring the data type of numbers numbers = new int[3]; //assigning the number of values numbers will contain int smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE; Scanner input = new Scanner(System.in); System.out.println("Please enter 3 numbers"); for(int counter = 0; counter<numbers.length;counter++) { numbers[counter] = input.nextInt(); } for(int i = 0; i<numbers.length; i++) { if(numbers[i]<smallest) smallest = numbers[i]; else if(numbers[i]>largest) largest = numbers[i]; } System.out.println("Largest is "+largest); System.out.println("Smallest is "+smallest); } } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.