Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* Lab Exercise 11.3 EmployeesThree.cpp Passing an array element to a function P

ID: 3763383 • Letter: #

Question

/* Lab Exercise 11.3

    EmployeesThree.cpp
    Passing an array element to a function
    Passing an array to a function

    Complete this exercise in 5 steps as follows:

    STEP 1: Prototype, define, and test InputEmployeeWage function
    STEP 2: Prototype, define, and test GetTotalWages function
    STEP 3: Prototype, define, and test GetLargestValue function
    STEP 4: Prototype, define, and test GetIndexOfSmallest function
    STEP 5: Prototype, define, and test PrintEmployeeList function

    Be sure to compile, fix errors (if any), and test
    after completing each step!
*/

#include<iostream>
#include<iomanip>
using namespace std;

/* TODO: Declare function prototypes during each step.

    NOTE: See comments following main() for function specifications
          regarding return types, function names, and parameter lists.
*/

//------------------------------------------------------------------------------

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 (STEP 1):

                Replace both statements 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 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 (STEPS 2, 3, 4):

            Replace initial values with value returning function calls.
            For each function, pass the employeeWages array and count, and
            assign the return value to the corresponding variable.
    */

    // STEP 2: Initialize total with return value from GetTotalWages function

    double total = 0.0;

    // STEP 3: Initialize largestValue with return value from GetLargestValue
    //          function

    double largestValue = 0.0;

    // STEP 4: Initialize indexOfSmallest with return value from
    //          GetIndexOfSmallest function

    int indexOfSmallest = 0;

    /* TODO (STEP 5):

        Replace FOR loop with one function call to PrintEmployeeList.
        Pass the employeeIds array, employeeWages array, and count.
    */

    for(int k = 0; k < count; k++)
    {
        cout << employeeIds[k] << ": $"
             << setw(6) << employeeWages[k] << endl;

        total += employeeWages[k];

        if(employeeWages[k] > largestValue)
            largestValue = employeeWages[k];

        if(employeeWages[k] < employeeWages[indexOfSmallest])
            indexOfSmallest = k;
    }


    cout << " Average Wage: " << (total / count) << endl;

    cout << " Largest wage found is $" << largestValue << endl;

    cout << "Smallest wage found is for employee "
         << employeeIds[indexOfSmallest] << ": $"
         << employeeWages[indexOfSmallest] << endl;

    cout << " End Program - ";

    return 0;
}

//------------------------------------------------------------------------------

/* TODO (STEP 1): Define InputEmployeeWage function

    Function to prompt and input wages for one employee.

    Parameters:
        in: empId
    Precondition:
        empId is id for employee whose wages will be input
    Postcondition:
        returns a double value for input wages
*/

//------------------------------------------------------------------------------

/* TODO (STEP 2): Define GetTotalWages function

    Function to find the sum of wages for employees.

    Parameters:
        in: empWages    array containing wages type double
        in: count       integer count of how many array elements to sum
    Precondition:
        empWages contains values for employees who have been assigned wages.
        count is >= 0 AND <= size of array
    Postcondition:
        returns a double value for sum of count wages
*/

//------------------------------------------------------------------------------

/* TODO (STEP 3): Define GetLargestValue function

    Function to find largest wage for employees.

    Parameters:
        in: empWages    array contianing wages type double
        in: count       integer count of how many array elements to consider
    Precondition:
        empWages contains values for employees who have been assigned wages.
        count is >= 0 AND <= size of array
    Postcondition:
        returns a double value for largest of count wages
*/

//------------------------------------------------------------------------------

/* TODO (STEP 4): Define GetIndexOfSmallest function

    Function to find array index for smallest wage value.

    Parameters:
        in: empWages    array containing wages type double
        in: count       integer count of how many array elements to consider
    Precondition:
        empWages contains values for employees who have been assigned wages
        count is >= 0 AND <= size of array
    Postcondition:
        returns integer index value for smallest of count wages
*/

//------------------------------------------------------------------------------

/* TODO (STEP 5): Define PrintEmployeeList function

    Function to print the id and wages for each employee.

    Parameters:
        in: empIds      array containing employee integer ids
        in: empWages    array containing employee wages type double
        in: count       integer count of how many array elements to consider
    Precondition:
        empIds contains id values for employees
        empWages contains values for employees who have been assigned wages
        count is >= 0 AND <= size of array
    Postcondition:
        List of ids and wages has been printed for count employees
*/

//------------------------------------------------------------------------------

/*                      Sample Program Output with no more than MAX_EMP wages...

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...

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

/* Lab Exercise 11.3

EmployeesThree.cpp

Passing an array element to a function

Passing an array to a function

Complete this exercise in 5 steps as follows:

STEP 1: Prototype, define, and test InputEmployeeWage function

STEP 2: Prototype, define, and test GetTotalWages function

STEP 3: Prototype, define, and test GetLargestValue function

STEP 4: Prototype, define, and test GetIndexOfSmallest function

STEP 5: Prototype, define, and test PrintEmployeeList function

Be sure to compile, fix errors (if any), and test

after completing each step!

*/

double InputEmployeeWage(int);

double GetTotalWages(double [], int);

double GetLargestValue(double [], int);

int GetIndexOfSmallest(double [], int);

void PrintEmployeeList(int [], double [], int);

#include<iostream>

#include<iomanip>

using namespace std;

/* TODO: Declare function prototypes during each step.

NOTE: See comments following main() for function specifications

regarding return types, function names, and parameter lists.

*/

//------------------------------------------------------------------------------

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 (STEP 1):

   Replace both statements 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 employeeWages array

   */

employeeWages[count] = InputEmployeeWage(employeeIds[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 (STEPS 2, 3, 4):

   Replace initial values with value returning function calls.

   For each function, pass the employeeWages array and count, and

   assign the return value to the corresponding variable.

   */

// STEP 2: Initialize total with return value from GetTotalWages function

double total = GetTotalWages(employeeWages, count);

// STEP 3: Initialize largestValue with return value from GetLargestValue

// function

double largestValue = GetLargestValue(employeeWages, count);

// STEP 4: Initialize indexOfSmallest with return value from

// GetIndexOfSmallest function

int indexOfSmallest = GetIndexOfSmallest(employeeWages, count);

/* TODO (STEP 5):

   Replace FOR loop with one function call to PrintEmployeeList.

   Pass the employeeIds array, employeeWages array, and count.

   */

PrintEmployeeList(employeeIds, employeeWages, count);

  

cout << " Average Wage: " << (total / count) << endl;

cout << " Largest wage found is $" << largestValue << endl;

cout << "Smallest wage found is for employee "

<< employeeIds[indexOfSmallest] << ": $"

<< employeeWages[indexOfSmallest] << endl;

cout << " End Program - ";

return 0;

}

//------------------------------------------------------------------------------

/* TODO (STEP 1): Define InputEmployeeWage function

Function to prompt and input wages for one employee.

Parameters:

in: empId

Precondition:

empId is id for employee whose wages will be input

Postcondition:

returns a double value for input wages

*/

//------------------------------------------------------------------------------

double InputEmployeeWage(int empId) {

double wage;

cout << "Enter wages for employee "

<< empId << ": ";

cin >> wage;

return wage;

}

/* TODO (STEP 2): Define GetTotalWages function

Function to find the sum of wages for employees.

Parameters:

in: empWages array containing wages type double

in: count integer count of how many array elements to sum

Precondition:

empWages contains values for employees who have been assigned wages.

count is >= 0 AND <= size of array

Postcondition:

returns a double value for sum of count wages

*/

double GetTotalWages(double empWages[], int count) {

double sum=0;

for (int i=0; i<count; i++) {

sum+=empWages[i];

}

return sum;

}

//------------------------------------------------------------------------------

/* TODO (STEP 3): Define GetLargestValue function

Function to find largest wage for employees.

Parameters:

in: empWages array contianing wages type double

in: count integer count of how many array elements to consider

Precondition:

empWages contains values for employees who have been assigned wages.

count is >= 0 AND <= size of array

Postcondition:

returns a double value for largest of count wages

*/

double GetLargestValue(double empWages[], int count) {

double max=empWages[0];

for (int i=1; i<count; i++) {

if (max < empWages[i]) {

max = empWages[i];

}

}

return max;

}

//------------------------------------------------------------------------------

/* TODO (STEP 4): Define GetIndexOfSmallest function

Function to find array index for smallest wage value.

Parameters:

in: empWages array containing wages type double

in: count integer count of how many array elements to consider

Precondition:

empWages contains values for employees who have been assigned wages

count is >= 0 AND <= size of array

Postcondition:

returns integer index value for smallest of count wages

*/

int GetIndexOfSmallest(double empWages[], int count) {

int index=0;

double min=empWages[index];

for (int i=1; i<count; i++) {

if (min > empWages[i]) {

min=empWages[i];

index=i;

}

}

return index;

}

//------------------------------------------------------------------------------

/* TODO (STEP 5): Define PrintEmployeeList function

Function to print the id and wages for each employee.

Parameters:

in: empIds array containing employee integer ids

in: empWages array containing employee wages type double

in: count integer count of how many array elements to consider

Precondition:

empIds contains id values for employees

empWages contains values for employees who have been assigned wages

count is >= 0 AND <= size of array

Postcondition:

List of ids and wages has been printed for count employees

*/

void PrintEmployeeList(int empIds[], double empWages[], int count) {

for(int k = 0; k < count; k++)

{

cout << empIds[k] << ": $"

<< setw(6) << empWages[k] << endl;

}

}