C++ PROGRAMMING Parallel Arrays Write a program that calculates the weekly pay f
ID: 3821433 • Letter: C
Question
C++ PROGRAMMING
Parallel Arrays
Write a program that calculates the weekly pay for a group of employees.
Your program should do the following:
1. Declare the following 4 parallel arrays, size 4.
• Names (String)
• hoursWorked (double)
• hourlyPay (double)
• WeeklyEarnings (double)
2. Use a for loop to prompt for the names, hours worked and hourly pay
for the 4 employees.
Example Output:
Please enter name #1: John
Please enter the number of hours worked: 10
Please enter the hourly pay rate: 12.50
Please enter name #2: Jane
Please enter the number of hours worked: 20
Please enter the hourly pay rate: 15.50
3. Use a for loop to calculate the total earnings for each employee and
store the results in the WeeklyEarnings array.
4.Use a for loop to display the results as follows:
Name Hours Worked Hourly Pay Weekly Earnings
--------------------------------------------------------
John 10 $12.50 $125.00
Jane 20 $15.50 $310.00
Mike 15 $10.00 $300.00
Carol 30 $13.00 $390.00
5. Extra Credit
Declare a global constant to represent the size of the arrays.
Use functions to complete steps 2,3 and 4.
Use the following function prototypes:
• GetInfo(string[], double[], double[]): void
• CalculateWeeklyEarnings(double[], double[], double[]): void
• DisplayInfo(string[], double[], double[], double[]): void
Explanation / Answer
#include <iostream>
using namespace std;
#define SIZE 4
void GetInfo(string names[], double hoursWorked[], double hourlyPay[]) {
for(int i=0; i<SIZE; i++){
cout<<"Please enter name #"<<(i+1)<<": ";
cin >> names[i];
cout<<"Please enter the number of hours worked: ";
cin >> hoursWorked[i];
cout<<"Please enter the hourly pay rate: ";
cin >> hourlyPay[i];
}
}
void CalculateWeeklyEarnings(double hoursWorked[], double hourlyPay[], double WeeklyEarnings[]){
for(int i=0; i<SIZE; i++){
WeeklyEarnings[i] = hoursWorked[i]* hourlyPay[i];
}
}
void DisplayInfo(string names[], double hoursWorked[], double hourlyPay[], double WeeklyEarnings[]) {
cout<<"Name Hours Worked Hourly Pay Weekly Earnings"<<endl;
for(int i=0; i<SIZE; i++){
cout<<names[i]<<" "<<hoursWorked[i]<<" $"<<hourlyPay[i]<<" $"<<WeeklyEarnings[i]<<endl;
}
}
int main()
{
string names[SIZE];
double hoursWorked[SIZE];
double hourlyPay[SIZE];
double WeeklyEarnings[SIZE];
GetInfo(names, hoursWorked, hourlyPay);
CalculateWeeklyEarnings( hoursWorked, hourlyPay, WeeklyEarnings);
DisplayInfo(names, hoursWorked, hourlyPay, WeeklyEarnings);
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Please enter name #1: John
Please enter the number of hours worked: 10
Please enter the hourly pay rate: 12.50
Please enter name #2: Jane
Please enter the number of hours worked: 20
Please enter the hourly pay rate: 15.50
Please enter name #3: Mike
Please enter the number of hours worked: 15
Please enter the hourly pay rate: 10
Please enter name #4: Carol
Please enter the number of hours worked: 30
Please enter the hourly pay rate: 13.0
Name Hours Worked Hourly Pay Weekly Earnings
John 10 12.5 125
Jane 20 15.5 310
Mike 15 10 150
Carol 30 13 390
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.