Function to Print Income of Employees with Name in Descending Order of Income

Hey C++ Friends,

So in my PrintData function i have printed out the employees and their new incomes displayed as required by my assignment. However the last part has been causing me a bit of a headache. I am required to SORT employee information by descending new Income before printing.

I realize this may be solved by a swap by reference or swap by pointers in rearranging essentially the rows of the data. I'm so lost as to the application of passing by reference or pointers to rearrange the data. The code should be in the print data function. If you could even help guide my thinking direction it would be much appreciated. Thanks!

So the table should be sorted to print like this:

Name Title Current Income New Income
Mickey Mouse Executive Chef 55640 57420.48
Cinderella VP of Customer Service 48776 50092.95
Donald Duck CFO 41912.00 43001.71
Goofy VP of Customer Service 32968.00 33891.19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
#include <string>
#include <iostream>
#include <iomanip>

using namespace std; 

void GetData( string[], string[], double[],double[]);
void CalcData(int, int, double[],double[],double[], double[]);
void PrintData(string ename[4], string etitle[4], double curr_year[], double new_year[]);


int main()
{

int const hours = 40;
int const weeks = 52;

string ename[4];
string etitle[4];

double ewage[4]; 
double inc[4]; 
double curr_year[4];
double new_year[4];
GetData(ename,etitle, ewage, inc);
CalcData(hours, weeks, ewage, inc,curr_year, new_year);
PrintData(ename, etitle, curr_year, new_year);

cout<<endl; system("PAUSE");
return 0;

}

/* Function Definitions */
void GetData(string fename[], string fetitle[], double fewage[],double finc[])
{
	for (int i = 0; i< 4; i++)
	{
		cout<<"Please enter Employee Name: "<<endl;
		getline(cin,fename[i]);
		cout<<"Please enter Employee Title: "<<endl;
		getline(cin,fetitle[i]);
		cout<<"Please enter Employee Hourly Wage: $"<<endl;
		cin>>fewage[i];
		cout<<"Please enter Employee Wage Increase: % "<<endl;
		cin>>finc[i];
		cin.ignore(80,'\n');
	}
}
void CalcData(int hours, int weeks, double fewage[],double finc[],double fcurr_year[], double fnew_year[])
{
	for (int i=0; i<4; i++)
	{
		int hours = 40;
		int weeks = 52;
		fcurr_year[i] = fewage[i] * hours * weeks;
		fnew_year[i] = fcurr_year[i] * (( finc[i] + 100 ) / 100);
	}
}
void PrintData(string fename[4], string fetitle[4], double fcurr_year[], double fnew_year[])
{		
		cout << left << setw(20)<<"Name";
		cout << left << setw(30)<<"Title";
		cout << left << setw(15)<<"Current Income";
		cout << left << setw(15)<<"New Income";
		cout << "\n" << endl;

// code goes here to sort??

	for (int i=0; i <4; i++)
	{
		cout << fixed;
		cout << setprecision(2);
		cout << left << setw(20) << fename[i];
		cout << left << setw(30) << fetitle[i];
		cout << left << setw(15) << fcurr_year[i];
		cout << left << setw(15) << fnew_year[i];
		cout << "\n" << endl;
	}
}


Please enter Employee Name:
Goofy
Please enter Employee Title:
Executive Chef
Please enter Employee Hourly Wage: $
15.85
Please enter Employee Wage Increase: %
2.8
Please enter Employee Name:
Mickey Mouse
Please enter Employee Title:
VP of Entertainment
Please enter Employee Hourly Wage: $
26.75
Please enter Employee Wage Increase: %
3.2
Please enter Employee Name:
Donald Duck
Please enter Employee Title:
CFO
Please enter Employee Hourly Wage: $
20.15
Please enter Employee Wage Increase: %
2.6
Please enter Employee Name:
Cinderella
Please enter Employee Title:
VP of Customer Service
Please enter Employee Hourly Wage: $
23.45
Please enter Employee Wage Increase: %
2.7
Name                Title                         Current Income New Income


Goofy               Executive Chef                32968.00       33891.10


Mickey Mouse        VP of Entertainment           55640.00       57420.48


Donald Duck         CFO                           41912.00       43001.71


Cinderella          VP of Customer Service        48776.00       50092.95



Press any key to continue . . .
I'd have a new function like SortData to make things easier to upgrade in the future should u want to have the option of different sorting like ascending/descending or alphabetically...

Maybe u'r not there yet but u'll probably want a structure to contain each of name/title/income/rate/etc.

Sorting everything is simple is you use the income as the index for sorting. So basically, wherever the income goes, everything else for that position does too.
1
2
3
4
5
6
7
8
9
10
11
12
13
//this sorts the incomes (unchecked!) in descending order
for(int i=0; i<length; i++)
{
  for(int j=0; j<(length-1); j++) 
  {
    if(income[j]<income[j+1])
    { //this is where names/titles/rates/etc should get sorted too, along with income...
      temp=income[j];
      income[j]=income[j+1];
      income[j+1]=temp;
    }
  }
}


strings can be sorted in the same way. doesn't have to be special:
1
2
3
4
string strtemp = "";
      strtemp=name[j];
      name[j]=name[j+1];
      name[j+1]=strtemp;
Cant begin to tell you how thankful i am for your help. Can you add friends in this? Hahaha. The program works beautifully :) I see now how helpful it would be to have used struct and will try it in my next program! Thankkk youu so muchh. =D
u'r welcome XD
Topic archived. No new replies allowed.