/* Homework Lab 33 Passing an array element to a function Passing an array to a
ID: 3827392 • Letter: #
Question
/* Homework Lab 33
Passing an array element to a function
Passing an array to a function
Complete this exercise in 5 steps (with substeps a, b, and c) as follows.
Complete each step (parts A, B, and C) one at a time and TEST before proceeding
to the next step to be sure what you did works.
STEP 1: Prototype (above main), define (under main), and call (in main) InputEmployeeWage function
STEP 2: Prototype(a), define(b), and call/test(c) GetTotalWages function
STEP 3: Prototype(a), define(b), and call/test(c) GetLargestValue function
STEP 4: Prototype(a), define(b), and call/test(c) GetIndexOfSmallest function
STEP 5: Prototype(a), define(b), and call/test(c) PrintEmployeeList function
Be sure to compile, fix errors (if any), and test
after completing each step!
*/
#include<iostream>
#include<iomanip>
using namespace std;
// TODO #1a: Declare function prototype
// Name: InputEmployeeWage
// Parameters: an int for the id of employee to enter wages for
// Returns: a double for the amount of wages entered
// NOTE: The purpose of this function is to get wages for ONE
// employee, not all of them.
// TODO #2a: Declare function prototype
// Name: GetTotalWages
// Parameters:
// - an array of type double containing the wages for all employees
// - an int containing a count of how many employees' wages were entered
// Returns: a double for the sum of the entered employees' wages
// NOTE: The purpose of this function is to sum wages for ALL
// employees, not one of them.
// TODO #3a: Declare function prototype
// Name: GetLargestValue
// Parameters:
// - an array of type double containing the wages for all employees
// - an int containing a count of how many employees' wages were entered
// Returns: a double for the wages of the employee who gets paid the most
// NOTE: The purpose of this function is to find the amount of money earned
// by the highest paid employee
// TODO #4a: Declare function prototype
// Name: GetIndexOfSmallest
// Parameters:
// - an array of type double containing the wages for all employees
// - an int containing a count of how many employees' wages were entered
// Returns: an int INDEX for the employee paid the least
// NOTE: The purpose of this function is to find the array INDEX for the
// location of the lowest paid employee
// TODO #5a: Declare function prototype
// Name: PrintEmployeeList
// Parameters:
// - an array of type int containing the ids for all employees
// - an array of type double containing the wages for all employees
// - an int containing a count of how many employees' wages were entered
// Returns: <nothing>
// NOTE: The purpose of this function is to print a chart of the id
// and wages for each employee that the user entered
//------------------------------------------------------------------------------
int main()
{
const int MAX_EMP = 5;
int employeeIds[MAX_EMP] = { 1001, 1002, 1003, 1004, 1005 };
double employeeWages[MAX_EMP];
char response;
int count = 0;
do
{
// TODO #1c: Replace the cout and cin below with one function call to
// InputEmployeeWage, where the next employee id in employeeIds
// array is passed to the function and the return value is assigned
// to the corresponding employee's wage in the employeeWages array.
cout << "Enter wages for employee "
<< employeeIds[count] << ": ";
cin >> employeeWages[count];
count++;
if (count < MAX_EMP)
{
cout << "Another (y or n): ";
cin >> response;
response = toupper(response);
}
} while (response == 'Y' && count < MAX_EMP);
cout << fixed << showpoint << setprecision(2);
cout << " ID Wage" << endl;
// TODO #2c: Replace the 0.0 in the initialization of totalWages below with
// the return value calculated by calling the GetTotalWages function
double totalWages = 0.0;
// TODO #3c: Replace the 0.0 in the initialization of largestWageValue below with
// the return value calculated by calling the GetLargestValue function
double largestWageValue = 0.0;
// TODO #4c: Replace the 0 in the initialization of indexOfSmallestWage below with
// the return value calculated by calling the GetIndexOfSmallest function
int indexOfSmallestWage = 0;
// TODO #5c: If you have copied and moved everything below properly in Steps 1-4
// and 5a and 5b, nothing should be left inside the for loop.
// Replace what remains of the FOR loop with one function call to the
// PrintEmployeeList function. Pass the employeeIds array,
// employeeWages array, and count.
for (int k = 0; k < count; k++) // Copy this for Steps 2b, 3b, 4b, and 5b
{
cout << employeeIds[k] << ": $" // Move this for Step 5b
<< setw(6) << employeeWages[k] << endl; // Move this for Step 5b
totalWages += employeeWages[k]; // Move this for Step 2b
if (employeeWages[k] > largestWageValue) // Move this for Step 3b
largestWageValue = employeeWages[k]; // Move this for Step 3b
if (employeeWages[k] < employeeWages[indexOfSmallestWage]) // Move this for Step 4b
indexOfSmallestWage = k; // Move this for Step 4b
}
cout << " Average Wage: " << (totalWages / count) << endl;
cout << " Largest wage found is $" << largestWageValue << endl;
cout << "Smallest wage found is for employee "
<< employeeIds[indexOfSmallestWage] << ": $"
<< employeeWages[indexOfSmallestWage] << endl;
cout << " End Program - ";
return 0;
}
//------------------------------------------------------------------------------
// TODO #1b: Define InputEmployeeWage function
// Purpose: Function to prompt and input wages for one employee.
// - Copy the prototype from Step 1a here - remove the ;
// - Use the code in main() above in Step 1c. You will need to
// modify that code some.
// TODO #2b: Define GetTotalWages function
// Purpose: Function to find the sum of wages for ALL employees.
// - Copy the prototype from Step 2a here - remove the ;
// - Use the code in main() above that says "Copy for Step 2b"
// and "Move for Step 2b" (located under Step 5c in main()).
// TODO #3b: Define GetLargestValue function
// Purpose: Function to find VALUE of the largest wage for entered employees.
// - Copy the prototype from Step 3a here - remove the ;
// - Use the code in main() above that says "Copy for Step 3b"
// and "Move for Step 3b" (located under Step 5c in main()).
// TODO #4b: Define GetIndexOfSmallest function
// Purpose: Function to find array INDEX for employee paid the least.
// - Copy the prototype from Step 4a here - remove the ;
// - Use the code in main() above that says "Copy for Step 4b"
// and "Move for Step 4b" (located under Step 5c in main()).
// TODO #5b: Define PrintEmployeeList function
// Purpose: Function to print the id and wages for every employee.
// - Copy the prototype from Step 5a here - remove the ;
// - Use the code in main() above that says "Copy for Step 5b"
// and "Move for Step 5b" (located under Step 5c in main()).
/*------------------------------------------------------------------------------
Sample Program Output with wages entered for MAX_EMP number of employees...
------------------------------------------------------------------------------
Enter wages for employee 1001: 10
Another (y or n): y
Enter wages for employee 1002: 20.50
Another (y or n): y
Enter wages for employee 1003: 15.25
Another (y or n): y
Enter wages for employee 1004: 24.10
Another (y or n): y
Enter wages for employee 1005: 9.95
ID Wage
1001: $ 10.00
1002: $ 20.50
1003: $ 15.25
1004: $ 24.10
1005: $ 9.95
Average Wage: 15.96
Largest wage found is $24.10
Smallest wage found is for employee 1005: $9.95
End Program - Press any key to continue . . .
------------------------------------------------------------------------------
Sample Program Output with fewer than MAX_EMP wages entered...
------------------------------------------------------------------------------
Enter wages for employee 1001: 10
Another (y or n): y
Enter wages for employee 1002: 20.50
Another (y or n): y
Enter wages for employee 1003: 15.25
Another (y or n): n
ID Wage
1001: $ 10.00
1002: $ 20.50
1003: $ 15.25
Average Wage: 15.25
Largest wage found is $20.50
Smallest wage found is for employee 1001: $10.00
End Program - Press any key to continue . . .
*/
<
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
double InputEmployeeWage(int employeeId);
double GetTotalWages(double employeeWages[], int count);
double GetLargestValue(double employeeWages[], int count);
int GetIndexOfSmallest(double employeeWages[], int count);
void PrintEmployeeList(int employeeIds[], double employeeWages[], int count);
int main()
{
const int MAX_EMP = 5;
int employeeIds[MAX_EMP] = { 1001, 1002, 1003, 1004, 1005 };
double employeeWages[MAX_EMP];
char response;
int count = 0;
do
{
employeeWages[count] = InputEmployeeWage(count);
count++;
if (count < MAX_EMP)
{
cout << "Another (y or n): ";
cin >> response;
response = toupper(response);
}
} while (response == 'Y' && count < MAX_EMP);
cout << fixed << showpoint << setprecision(2);
cout << " ID Wage" << endl;
double totalWages = GetTotalWages(employeeWages,MAX_EMP);
double largestWageValue = GetLargestValue(employeeWages,MAX_EMP);
int indexOfSmallestWage = GetIndexOfSmallest(employeeWages,MAX_EMP);
PrintEmployeeList(employeeIds, employeeWages, MAX_EMP);
cout << " Average Wage: " << (totalWages / count) << endl;
cout << " Largest wage found is $" << largestWageValue << endl;
cout << "Smallest wage found is for employee "
<< employeeIds[indexOfSmallestWage] << ": $"
<< employeeWages[indexOfSmallestWage] << endl;
cout << " End Program - ";
return 0;
}
double InputEmployeeWage(int employeeId)
{
double employeeWage;
cout << "Enter wages for employee "
<< employeeId << ": ";
cin >> employeeWage;
return employeeWage;
}
double GetTotalWages(double employeeWages[], int count)
{
double totalWages = 0;
for (int k = 0; k < count; k++)
{
totalWages += employeeWages[k];
}
return totalWages;
}
double GetLargestValue(double employeeWages[], int count)
{
double largestWageValue = 0.0;
for (int k = 0; k < count; k++)
{
if (employeeWages[k] > largestWageValue)
largestWageValue = employeeWages[k];
}
return largestWageValue;
}
int GetIndexOfSmallest(double emoloyeeWages[], int count)
{
int indexOfSmallestWage = 0;
for (int k = 0; k < count; k++)
{
if (emoloyeeWages[k] < emoloyeeWages[indexOfSmallestWage])
indexOfSmallestWage = k;
}
return indexOfSmallestWage;
}
void PrintEmployeeList(int employeeIds[], double employeeWages[], int count)
{
for (int k = 0; k < count; k++)
{
cout << employeeIds[k] << ": $"
<< setw(6) << employeeWages[k] << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.