/* Homework Lab 32 Loop processing with arrays Keeping count of array elements t
ID: 3824542 • Letter: #
Question
/* Homework Lab 32
Loop processing with arrays
Keeping count of array elements that are being used
Parallel Arrays: Two or more arrays whose corresponding elements
refer to related information.
*/
#include<iostream>
#include<iomanip>
using namespace std;
//------------------------------------------------------------------------------
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 #1:
Write a statement to prompt the user for wages for
each employee using the id array, e.g.
Enter wages for employee 1001:
Then store the input value for wages in the proper array element.
Then increment the 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 total = 0.0;
double largestValue = 0.0;
int indexOfSmallest = 0;
for (int k = 0; k < count; k++)
{
/* TODO #2:
Write a statement to print the employee ID and wages for
each employee using both arrays, e.g.
1001: $10.00
You will need to use the loop counter k to index the arrays.
*/
// TODO #3: Add the employee's wages to the total accumulator.
// TODO #4: If the employee's wage (in an array) is larger than others (so far)
// then assign that VALUE to largestValue (a non-array variable).
// TODO #5: If the employee's wage is smaller than others (so far)
// then assign the INDEX of that element to indexOfSmallest.
}
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;
}
//------------------------------------------------------------
/* Sample Program Output with exactly 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
/* Homework Lab 32
Loop processing with arrays
Keeping count of array elements that are being used
Parallel Arrays: Two or more arrays whose corresponding elements
refer to related information.
*/
#include<iostream>
#include<iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main()
{
const int MAX_EMP = 5;
int employeeIds[MAX_EMP] = { 1001, 1002, 1003, 1004, 1005 };
double employeeWages[MAX_EMP];
char response;
char nextIteration;
do{
int count = 0;
do
{
double salary;
cout << "Enter wages for employee "<<employeeIds[count]<<":";// Prompt statement for user to enter input
cin>> salary;// Input value is assigning to a variable
employeeWages[count]=salary;// Salary is inserting into the array
count++;// Incrementing the count value
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 total = 0.0;
double largestValue = 0.0;
double smallestValue = employeeWages[0];
int indexOfSmallest = 0;
for (int k = 0; k < count; k++)
{
cout<<" "<<employeeIds[k]<<" "<< employeeWages[k];// Printing the values present in both arrays
total=total+employeeWages[k];// caluclating total salary of all employees
if(employeeWages[k]>largestValue)
{
largestValue=employeeWages[k];// Finding largest salary from an employee salary array
}
if(employeeWages[k]<smallestValue)
{
smallestValue=employeeWages[k];
indexOfSmallest=k;// Finding smallest salary index from employee salary array
}
}
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 - Press any key to continue..";
cin>>nextIteration;
}while(nextIteration);// Added d-while for repetative iterations. If you want you can remove if it is removed your menthod executes once and it will get stop.
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.