Averaging Based on Internal Calculation

Hi, hoping someone can point me on the right road here...

I'm working on a program that reads data from a .in file. The data is for a payroll program (I know...but it's a good start for beginners!). I have everything working as far as the data being read and the functions that calculate the necessary information (netpay, tax rate, etc.). The next step is to have the program also tell me the average dollar amount of the netpays it has calculated. I understand how to sum and average if a user inputs the numbers that are to be worked with but not how to do the same calculations using data that the program has created itself. Basically, I'd like the program to output the statement "The average net pay for this period is: <AvgNetPay>" after the rest of the output. Any help would be much appreciated. My current code is as follows. I'm using Dev C++ for a compiler.

#include <iostream>
#include <fstream> // File Input/Output Stream
#include<iomanip>
#include <windows.h>

using namespace std;

//Function Prototypes
int readalldata(char[][10], char[][15], long int[],char [],int[],float[],const int);
void findovertimehours(int[],float[],int);
void findovertimepay(float[],float[],float[],int);
void findregularhours(int[],int[],int);
void findregularpay(int[],float[],float[],int);
void findgrosspay(float[],float[],float[],int);
void findtaxrate(float[],float[],char[], int);
void findtaxamount(float[],float[],float[],int);
void findnetpay(float[],float[],float[],int);
void printalldata(char[][10], char[][15],long int[],char[],int[],float[],float[],float[],float[],float[],float[],float[],int);

int main(){
const int MAXSIZE=100; //For Maximum of 100 Employees

//Declaration of Variables
int n=0;
int i=0;
char FName[MAXSIZE][10], LName[MAXSIZE][15], mstat[MAXSIZE];
long int id[MAXSIZE];
int hoursworked[MAXSIZE];
float overtimehours[MAXSIZE];
int regularhours[MAXSIZE];
float hourlyrate[MAXSIZE], regularpay[MAXSIZE];
float overtimepay[MAXSIZE], grosspay[MAXSIZE];
float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];



//Function Calls
n=readalldata(FName, LName, id, mstat, hoursworked, hourlyrate, MAXSIZE); // Get All Data
findovertimehours(hoursworked,overtimehours, n);
findovertimepay(overtimehours, hourlyrate, overtimepay, n);
findregularhours(hoursworked, regularhours, n);
findregularpay(regularhours, regularpay, hourlyrate, n);
findgrosspay(regularpay, overtimepay, grosspay, n);
findtaxrate(grosspay, taxrate, mstat, n);
findtaxamount(grosspay, taxamount, taxrate, n);
findnetpay(grosspay, netpay, taxamount, n);
printalldata(FName,LName,id,mstat,hoursworked,hourlyrate,overtimehours,overtimepay,regularpay,grosspay,taxrate,netpay,n);



system("PAUSE");
} // End of Main

//Function Definitions
int readalldata(char FName[][10], char LName[][15], long int id[], char mstat[], int hoursworked[], float hourlyrate[], int n){
ifstream fin("payroll.in");
n=0;

while(fin>>FName[n]>>LName[n]>>id[n]>>mstat[n]>>hoursworked[n]>>hourlyrate[n]) n++;

fin.close();
return n;
} // End Read All Data

void findovertimehours(int hoursworked[],float overtimehours[], int n){
for(int i=0; i<n; i++){
if(hoursworked[i]>40)overtimehours[i]=hoursworked[i]-40;
else overtimehours[i]=0;
} // End For
} // End Find Overtime Hours

void findovertimepay(float overtimehours[], float hourlyrate[], float overtimepay[], int n){
for(int i=0; i<n; i++){
overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
} // End For
} // End Find Overtime Pay

void findregularhours(int hoursworked[], int regularhours[], int n){
for(int i=0; i<n; i++){
if(hoursworked[i]>40) regularhours[i]=40;
else regularhours[i]=hoursworked[i];
} // End For
} // End Find Regular Hours

void findregularpay(int regularhours[], float regularpay[], float hourlyrate[], int n){
for(int i=0; i<n; i++){
regularpay[i]=regularhours[i]*hourlyrate[i];
} // End For
} // End Find Regular Pay

void findgrosspay(float regularpay[], float overtimepay[], float grosspay[], int n){
for(int i=0; i<n; i++){
grosspay[i]=regularpay[i]+overtimepay[i];
} // End For
} // End Find Gross Pay

void findtaxrate(float grosspay[], float taxrate[], char mstat[], int n){
for(int i=0; i<n; i++){
if(grosspay[i]>1000.00)taxrate[i]=.30;
else if(grosspay[i]>800.00) taxrate[i]=.20;
else if(grosspay[i]>500.00) taxrate[i]=.10;
else taxrate[i]=0;
if (mstat[i]=='S') taxrate[i]=(taxrate[i]+.05);
else if (mstat[i]=='s') taxrate[i]=(taxrate[i]+.05);
else if (mstat[i]=='H' && grosspay[i]>500) taxrate[i]=(taxrate[i]-.05);
else if (mstat[i]=='h' && grosspay[i]>500) taxrate[i]=(taxrate[i]-.05);
else if (mstat[i]=='M') taxrate[i]=(taxrate[i]*1);
else if (mstat[i]=='m') taxrate[i]=(taxrate[i]*1);
} // End For
} // End Find Tax Rate


void findtaxamount(float grosspay[], float taxamount[], float taxrate[], int n){
for(int i=0; i<n; i++){
taxamount[i]=grosspay[i]*taxrate[i];
} // End For
} // End Find Tax Amount

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
for(int i=0; i<n; i++){
netpay[i]=grosspay[i]-taxamount[i];
} // End For
} // End Find Net Pay

void printalldata(char FName[][10], char LName[][15], long int id[], char mstat[], int hoursworked[],
float hourlyrate[], float overtimehours[], float overtimepay[], float regularpay[],
float grosspay[], float taxrate[], float netpay[], int n){

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);//Change Output Text to Green
cout<<"\t \t \t MY PAYROLL PROGRAM"<<endl<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//Change Output Text back to Grey

cout<<setw(8)<<left<<"FIRST"<<setw(9)<<"LAST"<<setw(6)<<"ID"<<setw(6)<<"STAT"<<setw(4)<<"HW"<<
setw(5)<<"HR"<<setw(4)<<"OTH"<<setw(6)<<"OTP"<<setw(8)<<"REGP"<<setw(8)<<"GROSS"<<setw(6)<<"TAX"<<
setw(8)<<"NET"<<endl;

cout<<setw(8)<<left<<"====="<<setw(9)<<"===== "<<setw(6)<<"===="<<setw(6)<<"===="<<setw(4)<<"==="<<
setw(5)<<"==="<<setw(4)<<"==="<<setw(6)<<"==="<<setw(8)<<"====="<<setw(8)<<"====="<<setw(6)<<"===="
<<setw(3)<<"====="<<endl;

for(int i=0; i<n;i++){
cout<<setw(8)<<FName[i]<<setw(9)<<LName[i]<<setw(6)<<id[i]<<setw(6)<<mstat[i]<<setw(4)<<hoursworked[i]
<<setw(5)<<hourlyrate[i]<<setw(4)<<overtimehours[i]<<setw(6)<<overtimepay[i]<<setw(8)<<regularpay[i]
<<setw(8)<<grosspay[i]<<setw(6)<<taxrate[i]<<setw(1)<<"$"<<netpay[i]
<<endl<<endl;

} // End For
} // End Print All Data
// End Source Code
closed account (Lv0f92yv)
Might use code tags with []
Last edited on
I figured it out.

Added:

float findaverage(float netpay[], int n){
for(int i=0; i<n;i++){

sum=sum + netpay[i];
c=c+1;
average = sum/c;
}//End For
}//End Findaverage

(after void findnetpay)

and added:

float sum=0;
float average=0;
float c=0;

(as global variables)

Once that was in I was able to add code to the output after the final For:

cout<<setw(8)<<"The Total Net Pay is: $"<<sum<<endl;
cout<<setw(8)<<"The Average Net Pay is: $"<<average<<endl;

Works great. Whew! I hate when I bang my head on the desk for a few hours. Now I'm off to see if I can get it to sort the netpay. Wish me luck!
Topic archived. No new replies allowed.