Jul 11, 2010 at 5:55am UTC
Hello all, hope someone can help me out with this. Can someone please help me add a selection sort to my payroll program. I want to sort the net pay (ascending) and have it display the net pay before and after sorting. Any help would be greatly appreciated!
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
ifstream fin;
int n;
char employeename[20];
int hoursworked,overtime;
double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
double totalnetpay,avgnetpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
void findavgnetpay();
void printheadings();
void printdata();
public:payroll();
~payroll();
void printreport(); };
payroll::payroll(){
fin.open("payroll.in"); }//CONSTRUCTOR
payroll::~payroll(){
fin.close(); }//DESTRUCTOR
void payroll::calculategrosspay(){
if(hoursworked > 40){
overtime=hoursworked-40;
regularpay=hoursworked*hourlyrate;
overtimepay=overtime*(hourlyrate*1.5);
grosspay=regularpay+overtimepay; }//IF
else{ grosspay=hoursworked*hourlyrate;
regularpay=grosspay;
overtimepay=0;}//ELSE
}//CALCULATEGROSSPAY
void payroll::calculatetax(){
taxrate=.30;
taxamount=grosspay*taxrate; }//CALCULATETAX
void payroll::calculatenetpay(){
netpay=grosspay-taxamount;
totalnetpay=totalnetpay+netpay;
}//CALCULATENETPAY
void payroll::findavgnetpay(){
avgnetpay= totalnetpay/n;
}//FINDAVGNETPAY
void payroll::printheadings(){
cout<<setw(40)<<"-PAYROLL PROGRAM-"<<endl;
cout<<"---------------------------------------------------------"<<endl;
cout<<" NAME HW HR OT-PAY GROSS TAX NETPAY"<<endl;
cout<<"---------------------------------------------------------"<<endl;
}//PRINTHEADINGS
void payroll::printdata(){
cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
cout<<setw(6)<<employeename<<setw(6)<<hoursworked<<setw(8)<<
hourlyrate<<setw(8)<<overtimepay<<setw(8)<<grosspay<<
setw(8)<<taxamount<<setw(8)<<netpay<<endl; }//PRINTDATA
void payroll::printreport(){
n=0; totalnetpay=0;
printheadings();
while(fin>>employeename>>hoursworked>>hourlyrate){
calculategrosspay();
calculatetax();
calculatenetpay();
printdata();
n++; }//WHILE
findavgnetpay();
cout<<endl<<"The average net pay for "<<n<<" employees is "<<avgnetpay<<endl;
}//PRINTREPORT
int main(){
payroll employee;
employee.printreport();
system ("pause");
}//MAIN
Jul 11, 2010 at 6:20pm UTC
Here is a function I wrote up to help you sort it in ascending order. There is 2 though the other does the swapping for you. I hope this helps.
void sorting_an_array(int numbers[])
{
for(int index = 0; index < MAX - 1; index++)
{
for(int index_2 = index + 1; index_2 < MAX; index_2++)
{
if(numbers[index_2] < numbers[index])
swap(numbers[index], numbers[index_2]);
}
}
}
void swap(int& x, int& y)
{
int temporary;
temporary = x;
x = y;
y = temporary;
}
Jul 12, 2010 at 9:27pm UTC
Great! thank you so much! one more question though...at what point within the program do I insert the function?
Jul 12, 2010 at 9:33pm UTC
after you are all done with doing your statements with that array. Then you want to use the function. Just before you get ready to output that array.
Jul 12, 2010 at 9:35pm UTC
Also...does "max" have to be declared in the beginning? Thanks for the help!
Jul 12, 2010 at 10:12pm UTC
Yes declare it just after using name space std;
should look like this
const int MAX = 59;
or any number of your choice for how many array elements. Not only that but say if you want that array to hold a different amount of elements its easy to change just change the number
Jul 12, 2010 at 10:14pm UTC
so where you say char employee name 20 in the brace make it MAX2. So if you wanted to change anything it makes it easy and you can refer to max for your loops.