Sorting with pointers

I am struggling with sorting, this time with pointers. Can you guys give me some feedback? I am getting an error on transforming float to double

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
 #include<iostream>
#include<fstream>  
#include <iomanip>  	// file input output stream
	using namespace std; 


	struct person{
           char ms;
	      long int id;
	      int    hoursworked;
	      int    overtimehours;
	      int    regularhours;
	      float hourlyrate;
	      float regularpay;
	      float overtimepay;
	      float grosspay;
	      float taxrate;
	      float taxamount;
          float netpay;             };    // structure declaration
int  readalldata( person[ ], const int );           // function prototypes
	void findovertimehours( person[ ], int ); 
	void findovertimepay( person[ ], int ); 
	void findregularhours( person[ ], int );
	void findregularpay( person[ ], int );
	void findgrosspay( person[ ], int );
	void findtaxrate( person[ ], int );
	void findtaxamount( person[ ], int );
	void findnetpay( person[ ], int );
	void printalldata( person[ ], int );
	
	void sortbypointers(person[ ], int);

	
	       int main( ){
              
      const int MAXSIZE = 100;   	// for maximum of 100 employees
      int n;             //declaration of variables
      person employee[ MAXSIZE ];        
      n = readalldata( employee, MAXSIZE ); //functions calls
	      findovertimehours( employee, n );
	      findovertimepay( employee, n );
	      findregularhours( employee, n  );
      findregularpay( employee, n );
      findgrosspay( employee, n );
	      findtaxrate( employee, n );    
      findtaxamount( employee, n );
      findnetpay( employee, n );
      sortbypointers(employee,n);
      printalldata( employee, n );     
         
	      return 0; 
}//MAIN
// function definitions
 int readalldata( person emp[ ], int n ){
     ifstream fin( "employee9.in" );
      n = 0;
     while( fin >> emp[ n ].id >> emp[ n ].hoursworked >> emp[ n ].hourlyrate ){   
          n++;   }//WHILE
     fin.close( );
      return n;
 }//READALLDATA
 void findovertimehours( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
           if( emp[ i ].hoursworked > 40 )
                emp[ i ].overtimehours = emp[ i ].hoursworked - 40;
          else
               emp[ i ].overtimehours = 0;
     }//FOR
 }//FINDOVERTIMEHOURS
 void findovertimepay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++){
          emp[ i ].overtimepay = emp[ i ].overtimehours * emp[ i ].hourlyrate * 1.5;
      }//FOR
 }//FINDOVERTIMEPAY
 void findregularhours( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
           if( emp[ i ].hoursworked > 40 ) emp[ i ].regularhours = 40;
         else   emp[ i ].regularhours = emp[ i ].hoursworked;
     }//FOR
}//FINDREGULARHOURS
 void findregularpay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++ ){
          emp[ i ].regularpay = emp[ i ].regularhours * emp[ i ].hourlyrate;
     }//FOR
}//FINDREGULARPAY
 void findgrosspay( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
          emp[ i ].grosspay = emp[ i ].regularpay + emp[ i ].overtimepay;   
      }//FOR
}//FINDGROSSPAY
 void findtaxrate( person emp[ ], int n ){
    for( int i = 0 ; i < n ; i++){
          if( emp[ i ].grosspay > 1000.00 ) emp[ i ].taxrate = 0.30; 
          else if( emp[ i ].grosspay >800.00 ) emp[ i ].taxrate = 0.20;
          else if( emp[ i ].grosspay > 500.00 ) emp[ i ].taxrate = 0.10;
          else emp[ i ].taxrate = 0.0;
          
             if (emp[ i ].ms=='S') emp[ i ].taxrate=(emp[ i ].taxrate+.05);
                            else if (emp[ i ].ms=='s') emp[ i ].taxrate=(emp[ i ].taxrate+.05);
                            else if (emp[ i ].ms=='H' && emp[ i ].grosspay > 500.00 ) emp[ i ].taxrate=(emp[ i ].taxrate-.05);
                            else if (emp[ i ].ms=='h' && emp[ i ].grosspay > 500.00 ) emp[ i ].taxrate=(emp[ i ].taxrate-.05);
                            else emp[ i ].taxrate=(emp[ i ].taxrate*1);
                          
                            
      }//FOR
}//FINDTaXRATE
 void findtaxamount( person emp[ ], int n ){
     for( int i = 0 ; i < n ; i++){
           emp[ i ].taxamount = emp[ i ].grosspay * emp[ i ].taxrate;
      }//FOR
 }//FINDTAXAMOUNT
void findnetpay( person emp[ ], int n ){
      for( int i = 0 ; i < n ; i++){
          emp[ i ].netpay = emp[ i ].grosspay - emp[ i ].taxamount;
      }//FOR
 }//FINDNETPAY
 
 
void sortbypointers(person emp[ ], int n){ 
    int m= 5;
     float *p[m];
     int i,j;
     double *temp;
     int sortedflag=0;
 
  for(i=0;i<m;i++) p[i]=emp[ i ].netpay+i; //INITIALIZING POINTER ARRAY
  for(i=0;i<m;i++)cout<<*p[i]<<" ";
  while (!sortedflag){
   sortedflag=1;
   for(j=0;j<m-1;j++ ){  
      if (*p[j]>*p[j+1]){ 
        temp=p[j];
     p[j]=p[j+1];
     p[j+1]=temp;
     sortedflag=0;    }//SWAP
     }//J
     }//I   
       cout<<endl<<"SORTED ARRAY:";
       for(i=0;i<n;i++)cout<<*p[i]<<" ";
                                      }//sortfunction           

void printalldata( person emp[ ], int n ){
     cout << " ID" << "\t" << "HOURS" << "\t" << "RATE " << "\t " 
            << "OT PAY " << "\t" << "GROSS PAY" << "\t" << "TAX  " << "\t" 
           << "  NETPAY  " << endl;
     for( int i = 0 ; i < n; i++){
          cout << " " << emp[ i ].id << "\t " << emp[ i ].hoursworked 
                  << "\t" << emp[ i ].hourlyrate << "\t " << emp[ i ].overtimepay 
                  << "\t\t"  << emp[ i ].grosspay << "\t\t" << emp[ i ].taxamount 
                 << "\t " << emp[ i ].netpay << endl;
    }//FOR
  system ("pause");}//PRINTALLDATA
 
 // end source code 
Last edited on
Indent your code. And cut out the unnecessary. (what line is the error? )

1
2
3
4
5
6
struct person{
  float netpay;
};

/*121*/ float *p[m]; //an array of pointers
/*126*/ for(i=0;i<m;i++) p[i]=emp[ i ].netpay+i; //cannot convert 'double' to 'float*' in assignment 
Why are you working with pointers?

Last edited on
The error is as you described- line 126, cant convert double to float. I am using pointers as part of a homework assignment
cannot convert 'double' to 'float*' in assignment. You are trying to assign to a pointer.
If you don't want side effects in the emp array use p[i] = &emp[i].netpay;
ok i have amended to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void sortbypointers(person emp[ ], int n){ 
  
     int i,j;
     float *p[n];
     double temp;
     int sortedflag=0;
 
for(i=0;i<n;i++) p[i]=&emp[ i ].netpay+i; 
  while (!sortedflag){
   sortedflag=1;
   for(j=0;j<n-1;j++ ){  
      if (*p[j]>*p[j+1]){ 
        temp=*p[j];
     *p[j]=*p[j+1];
     *p[j+1]=temp;
     sortedflag=0;    }
     }
     }  


I still can figure out whats wrong.
I still can figure out whats wrong.
Maybe not the cause, but you could tell us the effect.

p[i]=&emp[ i ].netpay+i; Why are you adding i? netpay is not an array.
Thanks for your note. I deleted the "+i" and it results in the netpay being sorted, but not the rest of the data (name, grosspay, etc..). Is there an error in my structure then?
hey i have a question, why do we use pointers for arrays? in functions they are called by reference without using pointers. why do we need pointers in arrays? thanks
My prof suggests I "sort the name as well" I am confused as the in file consists of ID's hours worked and hourly wage. Any ideas?
Topic archived. No new replies allowed.