Need to wirte four functions on array of worker stuctures. 1. one to input an ar
ID: 3567646 • Letter: N
Question
Need to wirte four functions on array of worker stuctures.
1. one to input an array of work structures - the ID number, hours worked, and hourly rate.
2. figure the earned for each worker structure in the array - calculate time and a half for all hours above 40. EX. $11 per hours and work 50 hr.earned would be 40 x $11 + 10 x $15
3. output the array of workers structures
4. count the numbr of workers who have worked overtime. Return answer through function call
struct worker{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned;
};
Explanation / Answer
#include <iostream>
using namespace std;
int totalSize = 5, numAllocated = 0, int Normal_Hours = 40;
struct worker{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned = 0;
};
void doubleSize(worker *&arr){
totalSize *= 2;
worker *newarr = new worker[totalSize];
for (int i = 0; i < numAllocated; ++i){
newarr[i] = arr[i];
}
arr = newarr;
}
void addWorker(worker *&arr){
if (totalSize == numAllocated){
doubleSize(arr);
}
int id;
int flag = 0;
cout << "idNumber of the worker is: ";
cin >> id;
arr[numAllocated].idNumber;
for (int i = 0; i < numAllocated; ++i){
if (id == arr[i].idNumber){
flag = 1;
break;
}
}
if (flag){
cout << id << " Already Exists" << endl;
}
else{
arr[numAllocated].idNumber = id;
cout << "Hours Worked: ";
cin >> arr[numAllocated].hoursWorked;
cout << "Hourly Rate: ";
cin >> arr[numAllocated].hourlyRate;
numAllocated++;
}
}
void WageEarned(worker *arr){
if (numAllocated == 0){
cout << "Array is Empty" << endl;
return;
}
for (int i = 0; i < numAllocated; ++i){
if (arr[i].hoursWorked > 40){
arr[i].earned = (40 * 11) + ((arr[i].hoursWorked - 40) * 15);
}
else{
arr[i].earned = arr[i].hourlyRate * arr[i].hoursWorked;
}
}
}
void outputWorkers(worker *arr){
if (numAllocated == 0){
cout << "Array is Empty" << endl;
return;
}
for (int i = 0; i < numAllocated; ++i){
cout << "Worker id: " << arr[i].idNumber << endl;
cout << "HoursWorked: " << arr[i].hoursWorked << endl;
cout << "HourlyRate: " << arr[i].hourlyRate << endl;
cout << "WageEarned: " << arr[i].earned << endl;
cout << endl << endl;
}
}
int countOvertimeWorked(worker *arr){
int hours = 40;
if (numAllocated == 0){
cout << "Array is Empty" << endl;
return -1;
}
int count = 0;
for (int i = 0; i < numAllocated; ++i){
if (arr[i].hoursWorked > hours){
count++;
}
}
return count;
}
int main(){
int Normal_hours = 4;
int option;
worker *arr = new worker[totalSize];
while (true){
cout << "Enter 1 to add Worker" << endl;
cout << "Enter 2 to display Workers" << endl;
cout << "Enter 3 to Count Number of overWorked Workers" << endl;
cout << "Enter 4 to calculate Wages" << endl;
cout << "Enter 5 to exit" << endl;
cin >> option;
switch (option){
case 1:
addWorker(arr);
break;
case 2:
outputWorkers(arr);
break;
case 3:
cout << "Enter Normal Work Rate: ";
cin >> Normal_hours;
cout << "Number of Workers worked overtime are " << countOvertimeWorked(arr) << endl;
break;
case 4:
WageEarned(arr);
cout << "Wages Calculated" << endl;
break;
case 5:
return 0;
default:
cout << "Invalid Input" << endl;
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.