Sorting with Multiple Columns

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <fstream> // File Input/Output Stream
#include<iomanip>
#include <windows.h>

using namespace std;

float sum=0;
float average=0;
float c=0;
int n=0;

int readalldata(char[][10], char[][15], long int[],char [],int[],float[],const int);
void findoth(int[],float[],int);
void findotp(float[],float[],float[],int);
void findregh(int[],int[],int);
void findregp(int[],float[],float[],int);
void findgp(float[],float[],float[],int);
void findtaxr(float[],float[],char[], int);
void findtaxa(float[],float[],float[],int);
void findnp(float[],float[],float[],int);
float findaverage(float[],int);
int sort(char[][10], char[][15],long int[],char[],int[],float[],float[],float[],float[],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;      
     
     
     int i=0;
     char FName[MAXSIZE][10], LName[MAXSIZE][15], mstat[MAXSIZE];
     long int id[MAXSIZE];
     int hw[MAXSIZE];
     float oth[MAXSIZE];
     int regh[MAXSIZE];
     float hr[MAXSIZE], regp[MAXSIZE];
     float otp[MAXSIZE], gp[MAXSIZE];
     float taxr[MAXSIZE], taxa[MAXSIZE], np[MAXSIZE];
     
     n=readalldata(FName, LName, id, mstat, hw, hr, MAXSIZE); 
     findoth(hw,oth, n);
     findotp(oth, hr, otp, n);
     findregh(hw, regh, n);
     findregp(regh, regp, hr, n);
     findgp(regp, otp, gp, n);
     findtaxr(gp, taxr, mstat, n);
     findtaxa(gp, taxa, taxr, n);
     findnp(gp, np, taxa, n);
     findaverage(np,n);
     printalldata(FName,LName,id,mstat,hw,hr,oth,otp,regp,gp,taxr,np,n);
     sort(FName,LName,id,mstat,hw,hr,oth,otp,regp,gp,taxr,np,n);
   
 
    
  
   system("PAUSE");
} 

    int readalldata(char FName[][10], char LName[][15], long int id[], char mstat[], int hw[], float hr[], int n){
    ifstream fin("payroll.in");
    n=0;
    
    while(fin>>FName[n]>>LName[n]>>id[n]>>mstat[n]>>hw[n]>>hr[n]) n++;
    
    fin.close();
    return n;
} 

void findoth(int hw[],float oth[], int n){
     for(int i=0; i<n; i++){
        if(hw[i]>40)oth[i]=hw[i]-40;
           else oth[i]=0;
     } 
} 

void findotp(float oth[], float hr[], float otp[], int n){
     for(int i=0; i<n; i++){
         otp[i]=oth[i]*hr[i]*1.5;
     }    
} 

void findregh(int hw[], int regh[], int n){
     for(int i=0; i<n; i++){     
        if(hw[i]>40) regh[i]=40;
           else regh[i]=hw[i];
     } 
} 
void findregp(int regh[], float regp[], float hr[], int n){
      for(int i=0; i<n; i++){
         regp[i]=regh[i]*hr[i];
      } 
} 

void findgp(float regp[], float otp[], float gp[], int n){
      for(int i=0; i<n; i++){
         gp[i]=regp[i]+otp[i];
      } 
} 

void findtaxr(float gp[], float taxr[], char mstat[], int n){
      for(int i=0; i<n; i++){
         if(gp[i]>1000.00)taxr[i]=.30;
            else if(gp[i]>800.00) taxr[i]=.20;
               else if(gp[i]>500.00) taxr[i]=.10;
                 else taxr[i]=0;
                        if (mstat[i]=='S') taxr[i]=(taxr[i]+.05);
                            else if (mstat[i]=='s') taxr[i]=(taxr[i]+.05);
                            else if (mstat[i]=='H' && gp[i]>500) taxr[i]=(taxr[i]-.05);
                            else if (mstat[i]=='h' && gp[i]>500) taxr[i]=(taxr[i]-.05);
                            else if (mstat[i]=='M') taxr[i]=(taxr[i]*1);
                            else if (mstat[i]=='m') taxr[i]=(taxr[i]*1);
      } 
} 


void findtaxa(float gp[], float taxa[], float taxr[], int n){
      for(int i=0; i<n; i++){
          taxa[i]=gp[i]*taxr[i];
      } 
} 

void findnp(float gp[], float np[], float taxa[], int n){
      for(int i=0; i<n; i++){
          np[i]=gp[i]-taxa[i];
      }                                              
} 


float findaverage(float np[], int n){
     for(int i=0; i<n;i++){ 
        
        sum=sum + np[i];
        c=c+1;
        average = sum/c;
        }
     }
     
int sort(char FName[][10], char LName[][15], long int id[], char mstat[], int hw[], 
         float hr[], float oth[], float otp[], float regp[],
         float gp[], float taxr[], float np[], int n){
    int i,j;    
    for(int i=0; i<n-1; i++){
        for(j=n-1; j>i; j--){
            if(np[j]<np[j-1]){
               float hold = (FName[j][10], LName[j][15], id[j], mstat[j], hw[j], 
                             hr[j], oth[j], otp[j], regp[j],
                             gp[j], taxr[j], np[j]);
               (FName[j][10], LName[j][15], id[j], mstat[j], hw[j], 
               hr[j], oth[j], otp[j], regp[j],
               gp[j], taxr[j], np[j]) = (FName[j-1][10], LName[j-1][15], id[j-1], mstat[j-1], hw[j-1], 
               hr[j-1], oth[j-1], otp[j-1], regp[j-1], gp[j-1], taxr[j-1], np[j-1]);
               (FName[j-1][10], LName[j-1][15], id[j-1], mstat[j-1], hw[j-1], 
                hr[j-1], oth[j-1], otp[j-1], regp[j-1],
                gp[j-1], taxr[j-1], np[j-1]) = hold;
          }//
       }//
    }//
      cout<<"Sorted Data"<<endl<<endl;
      
      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(i=0;i<n;i++){
       cout<<setw(8)<<FName[i]<<setw(9)<<LName[i]<<setw(6)<<id[i]<<setw(6)<<mstat[i]<<setw(4)<<hw[i]
         <<setw(5)<<hr[i]<<setw(4)<<oth[i]<<setw(6)<<otp[i]<<setw(8)<<regp[i]
         <<setw(8)<<gp[i]<<setw(6)<<taxr[i]<<setw(1)<<"$"<<np[i]
         <<endl<<endl;    
       }
}



void printalldata(char FName[][10], char LName[][15], long int id[], char mstat[], int hw[], 
                  float hr[], float oth[], float otp[], float regp[],
                  float gp[], float taxr[], float np[], int n){
                        
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
      cout<<"\t \t \t MY PAYROLL PROGRAM"<<endl<<endl;
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
      
      cout<<"Unsorted Data"<<endl<<endl;
      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)<<hw[i]
         <<setw(5)<<hr[i]<<setw(4)<<oth[i]<<setw(6)<<otp[i]<<setw(8)<<regp[i]
         <<setw(8)<<gp[i]<<setw(6)<<taxr[i]<<setw(1)<<"$"<<np[i]
         <<endl<<endl;    
       
       } 
       cout<<setw(8)<<"The Total Net Pay is: $"<<sum<<endl; 
       cout<<setw(8)<<"The Average Net Pay is: $"<<average<<endl<<endl;
} 


My data all calcs properly. My non-sorted data is fine. Sorted is wrong. It only sorts Netpay column (leaves the others in same order). How do I get the output to keep all of the related columns with the netpay and still sort by netpay?

Example:

Unsorted Data

Joe Smith 1234 M 43 10 3 45 400 445 0 $445
Mary Jones 2345 S 40 12.75 0 0 510 510 0.15 $433.5

As is Sorted Output comes out:

Joe Smith 1234 M 43 10 3 45 400 445 0 $433.5
Mary Jones 2345 S 40 12.75 0 0 510 510 0.15 $445

Should come out:

Mary Jones 2345 S 40 12.75 0 0 510 510 0.15 $433.5
Joe Smith 1234 M 43 10 3 45 400 445 0 $445

Any ideas would be great.
Thanks!
Last edited on
First, thank you for pointing me to the code tags article. I fixed that issue.

2nd, I'm not sure I understand how the second article relates to my issue. It appears as if the article is creating an array (a group of integers) and sorting it. I can sort a set of integers. This program already does that. I'm trying to understand how to expand the sort so that it contains all of the different variables that have been defined by the program (either by declaration or by calculation).
Ok. Instead of using a bunch of arrays you could make a struct that holds that information for one person and then make an array of such structs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Person
{
    //use std::string to
    //hold strings

    string first_name;
    string last_name;
    char mstat;
    //etc...
};

//...

Person people[MAXSIZE];

Info on structs -> http://cplusplus.com/doc/tutorial/structures/
Info on std::string -> http://cplusplus.com/reference/string/string/

I don't doubt your ability to write good sorting functions :) but std::sort offers more flexibility.
Let's see how it works...

Suppose you've loaded the data on your people array. What do you want to sort it by?

Last name?

1
2
3
4
5
6
7
8
bool SortByLastName(const Person & p1, const Person & p2)
{
    return p1.last_name<p2.last_name;
}

//...

sort(people,people+n,SortByLastName);

Netpay?

1
2
3
4
5
6
7
8
bool SortByNetPay(const Person & p1, const Person & p2)
{
    return p1.net_pay<p2.net_pay;
}

//...

sort(people,people+n,SortByNetPay);

See? So simple! :D
Here's a small example:

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
#include <iostream>
#include <string> //for std::string
#include <algorithm> //for std::sort
using namespace std;

struct Person
{
    string name;
    int age;

    Person(const char * s, int n):
        name(s),age(n){}
};

bool SortByName(const Person & p1,
    const Person & p2)
{
    return p1.name<p2.name;
}

bool SortByAge(const Person & p1,
    const Person & p2)
{
    return p1.age<p2.age;
}

void print(Person array[],int size)
{
    for (int i=0; i<size; i++)
    {
        cout << array[i].name << ' ';
        cout << array[i].age << endl;
    }
}

int main()
{
    Person people[3]=
    {
        Person("Shawn",20),
        Person("Bob", 30),
        Person("Mary",15)
    };

    cout << "unsorted:\n" << endl;
    print(people,3);

    sort(people,people+3,SortByName);

    cout << "\nsorted by nane:\n" << endl;
    print(people,3);

    sort(people,people+3,SortByAge);

    cout << "\nsorted by age:\n" << endl;
    print(people,3);

    cin.get();
    return 0;
}

Last edited on
Topic archived. No new replies allowed.