Sorting Netpay By The Pointers

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 3649 Views - Last Post: 09 August 2012 - 08:29 PM Rate Topic: -----

#1 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Sorting Netpay By The Pointers

Posted 03 August 2012 - 11:42 AM

Program 3 - Sort the netpay by the pointers
Sort the net pays (salary) using an array of pointers (do not change the data in the original array). For now, display only the net pays before sorting Sort the net pays (salary) using an array of pointers (do not change the data in the original array). For now, display only the net pays before sorting and after sorting.

My manager had me learn C++ basics and I made an entire payroll program using 5 employees. I first learned the main program, then functions, then average's, and sorting. Now the final part I have to do on this program that I've been working a month on now is Program 3 above. He wanted it due last week wed and this last part I'm having trouble with. Can somebody please help me out? Here's what I have with errors:

#include <iostream>
#include<fstream>//file input output stream
using namespace std; 

//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);
void sort(float[], int);
void pointer_array_sort(int*values);

int main(){
     const int MAXSIZE=50;
     int netpay[MAXSIZE];   //for maximum of 100 employees
     
     //decleration of variables
     int n;
     long int id[MAXSIZE];
     int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
     int regularhours[MAXSIZE];
     float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
     overtimepay[MAXSIZE], grosspay[MAXSIZE];
     float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
     
     //function calls
     n=readalldata(id, 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, n);
     findtaxamount(grosspay, taxamount, taxrate, n);
     findnetpay(grosspay, netpay, taxamount, n);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      sort(netpay, n);
                      printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      return 0;
}//MAIN
//function definitions
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
    ifstream fin("employee.txt");
    n=0;
    
    while(fin>>id[n]>>hoursworked[n]>>hourlyrate[n]) n++;
    fin.close();
    return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) overtimehours[i]=hoursworked[i]-40;
             else overtimehours[i]=0;
     }//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
     float overtimepay[], int n){
     for(int i=0; i<n; i++){
             overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
     }//FOR
}//FINDOVERTIMEPAY

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];
     }//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
                        float hourlyrate[], int n){
      for(int i=0; i<n; i++){
      regularpay[i] = regularhours[i]*hourlyrate[i];
      }//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
                        float grosspay[], int n){
                        for(int i=0; i<n; i++){
                        grosspay[i]=regularpay[i]+overtimepay[i];
        }//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
     for(int i=0; i<n; i++){
             if(grosspay[i]>4000.00) taxrate[i]=0.40;
             else if(grosspay[i]>3000.00) taxrate[i]=0.30;
             else if(grosspay[i]>1000.00) taxrate[i]=0.20;
             else taxrate[i]=0.10;
     }//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
                         float taxrate[], int n){
for(int i=0; i<n; i++){
        taxamount[i]=grosspay[i]*taxrate[i];
        }//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
     for(int i=0; i<n; i++){
          netpay[i]=grosspay[i]-taxamount[i];
     }//FOR
}//FINDNETPAY

void printalldata(long int id[], int hoursworked[], float hourlyrate[],
                       float overtimepay[], float grosspay[], float taxamount[],
                       float netpay[], int n){
                       float totalNetPay=0;
                       
                       int i=0;
                       cout<<"EMP ID"<<"\t"<<"HOURS"<<"\t"<<"RATE"<<"\t"
           <<"OVERPAY"<<"\t"<<"   GROSSPAY"<<"\t"<<"        TAX"<<"\t"
           <<"NETPAY"<<endl;
           
for(i; i<n; i++){
       totalNetPay +=netpay[i];
        cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
        <<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
        <<taxamount[i]<<"\t"<<netpay[i]<<endl;
        }//FOR
        cout<<"The net pay average of the employees are: "<<totalNetPay/i<<endl;
        system("PAUSE");
}//PRINTALLDATA
int*arraypointer=&netpay;
int sortednetpay=pointer_array_sort(arraypointer);
void pointer_array_sort(int*Values)
{
  int i, j;
  
for(i=0; i<n-1; i++){
    for(j=n-1; j>i; j--){
      if(table[j]<table[j-1]){
        float hold=table[j];
        table[j]=table[j-1];
        table[j-1]=hold;
      }// end if
    }// end j
  }// end i
}
//end source code  


Here's what I have with errors:

1.)C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `int main()':
2.)21 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp expected `,' or `;' before "int"
3.)21 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp At global scope:
4.)134 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `netpay' was not declared in this scope
5.)135 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp void value not ignored as it ought to be
6.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `void pointer_array_sort(int*)':
7.)140 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `n' undeclared (first use this function)
8.) (Each undeclared identifier is reported only once for each function it appears in.)
9.)142 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `table' undeclared (first use this function)

I made an example program using pointers to sort:

#include<iostream.h>
int main(){
     const int n=5;
     int item[n]={2,5,3,1,8};
     int *p[n];
int i,j;
     int*temp;
     int sortedflag=0;
     for(i=0;i<n;i++) p[i]=item+i;//INITIALIZING POINTER ARRAY
     for(i=0;i<n;i++)cout<<*p[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;      }//SWAP
                                                             }//J
                                                        }//I
     cout<<endl<<"SORTED ARRAY: ";
     for(i=0;i<n;i++)cout<<*p[i]<<" ";
     system("PAUSE");
     }//MAIN 


Here's my "employee.txt" :


8572 42 12.00
6423 40 15.00
7465 45 10.00
2477 40 16.00
5996 44 18.00

I am on the right track I believe but I have some serious problems. How to I incorporate the sample program to make my payroll program work? Once it comes to additing it into a program full of functions I get confused. Can somebody please help me out? I've been working on this every night and I have to finish it. Thank you for anybody who replies

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Netpay By The Pointers

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4067
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 11:59 AM

You seem to have two variables that you named netpay, one an array of float and one an array of int.
int main(){
     const int MAXSIZE=50;
     int netpay[MAXSIZE];   //for maximum of 100 employees
.......
     float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];



Quote

5.)135 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp void value not ignored as it ought to be
6.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `void pointer_array_sort(int*)':

Look at your function prototype and function call:

void pointer_array_sort(int*values);
...
int sortednetpay=pointer_array_sort(arraypointer);

You are trying to assign sortednetpay a value returned from a void function. A function defined to return void returns nothing, you can't use it in an assignment.

You also seem to have code outside any functions:

void printalldata(long int id[], int hoursworked[], float hourlyrate[],
                  float overtimepay[], float grosspay[], float taxamount[],
                  float netpay[], int n) {
............
}//PRINTALLDATA
// The following lines are not inside any function.
int*arraypointer=&netpay;
int sortednetpay=pointer_array_sort(arraypointer);

void pointer_array_sort(int*Values)
{



You may want to consider using a structure or class to group your information together.

Jim

This post has been edited by jimblumberg: 03 August 2012 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 12:24 PM

So my first step is to:

1.)Eliminate
int netpay[MAXSIZE]; 
at line 21?
2.)Eliminate
void pointer_array_sort(int*values);
in my function prototypes line 17?


Lines 134:

int*arraypointer=&netpay;
int sortednetpay=pointer_array_sort(arraypointer);
void pointer_array_sort(int*Values)


This is all in the wrong spot I presume as well?

I appreciate you trying to help but where do I go from here? Am I correct for question 1 and 2?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 12:30 PM

Yes, for #1.

As for #2, you have to decide whether you want your function to return something or not. Right now, the way your code is written, it doesn't return anything and it probably makes sense to keep it that way.

As Jim said, you should consider looking at using structs or classes instead of trying to maintain all your parallel arrays.
Was This Post Helpful? 0
  • +
  • -

#5 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 12:37 PM

The problem is. I'm learning to specifically do it this way with parallel arrays because I'm learning step by step and this is the next step. After I understand how to do it this way then I can evolve and get into structures. You know what I mean bud. So what's next now?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4067
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 12:44 PM

My suggestion is to move on to structures. Trying maintain the number of parallel arrays you're using is fraught with errors. If you want to study parallel arrays try to limit yourself to 3 of 4 at most.

But if you insist on this path, then fix the errors mentioned. Recompile, fix the next batch of errors, repeat until the program compiles without errors. You should be compiling after writing each line of code. Never try to implement the entire program before you try to compile your program. By compiling in small chunks will making fixing errors much easier because you know where to look.

If you don't understand your error messages post the complete error messages, exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing the errors. Always start with the first error, recompiling often. Many times fixing one problem will solve multiple error messages.

Also I don't provide one on one help via PM. Post your questions here in the forum that way your problems may help someone else in the future.


Jim

This post has been edited by jimblumberg: 03 August 2012 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 03:20 PM

Errors:
1.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `int main()':
2.)46 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp cannot convert `float*' to `int*' for argument `1' to `void pointer_array_sort(int*)'
3.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `void pointer_array_sort(int*)':
4.)139 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `n' undeclared (first use this function)
5.) (Each undeclared identifier is reported only once for each function it appears in.)
6.)139 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `p' undeclared (first use this function)
7.)139 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `item' undeclared (first use this function)

#include <iostream>
#include<fstream>//file input output stream
using namespace std; 

//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);
void sort(float[], int);
void pointer_array_sort(int*values);

int main(){
     const int MAXSIZE=50;  //for maximum of 100 employees
     
     //decleration of variables
     int n;
     long int id[MAXSIZE];
     int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
     int regularhours[MAXSIZE];
     float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
     overtimepay[MAXSIZE], grosspay[MAXSIZE];
     float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
     
     //function calls
     n=readalldata(id, 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, n);
     findtaxamount(grosspay, taxamount, taxrate, n);
     findnetpay(grosspay, netpay, taxamount, n);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      sort(netpay, n);
                      printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      pointer_array_sort(netpay);
                      return 0;
}//MAIN
//function definitions
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
    ifstream fin("employee.txt");
    n=0;
    
    while(fin>>id[n]>>hoursworked[n]>>hourlyrate[n]) n++;
    fin.close();
    return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) overtimehours[i]=hoursworked[i]-40;
             else overtimehours[i]=0;
     }//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
     float overtimepay[], int n){
     for(int i=0; i<n; i++){
             overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
     }//FOR
}//FINDOVERTIMEPAY

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];
     }//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
                        float hourlyrate[], int n){
      for(int i=0; i<n; i++){
      regularpay[i] = regularhours[i]*hourlyrate[i];
      }//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
                        float grosspay[], int n){
                        for(int i=0; i<n; i++){
                        grosspay[i]=regularpay[i]+overtimepay[i];
        }//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
     for(int i=0; i<n; i++){
             if(grosspay[i]>4000.00) taxrate[i]=0.40;
             else if(grosspay[i]>3000.00) taxrate[i]=0.30;
             else if(grosspay[i]>1000.00) taxrate[i]=0.20;
             else taxrate[i]=0.10;
     }//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
                         float taxrate[], int n){
for(int i=0; i<n; i++){
        taxamount[i]=grosspay[i]*taxrate[i];
        }//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
     for(int i=0; i<n; i++){
          netpay[i]=grosspay[i]-taxamount[i];
     }//FOR
}//FINDNETPAY

void printalldata(long int id[], int hoursworked[], float hourlyrate[],
                       float overtimepay[], float grosspay[], float taxamount[],
                       float netpay[], int n){
                       float totalNetPay=0;
                       
                       int i=0;
                       cout<<"EMP ID"<<"\t"<<"HOURS"<<"\t"<<"RATE"<<"\t"
           <<"OVERPAY"<<"\t"<<"   GROSSPAY"<<"\t"<<"        TAX"<<"\t"
           <<"NETPAY"<<endl;
           
for(i; i<n; i++){
       totalNetPay +=netpay[i];
        cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
        <<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
        <<taxamount[i]<<"\t"<<netpay[i]<<endl;
        }//FOR
        cout<<"The net pay average of the employees are: "<<totalNetPay/i<<endl;
        system("PAUSE");
}//PRINTALLDATA
void pointer_array_sort(int*Values)
{
  int i, j;
int*temp;
     int sortedflag=0;
     for(i=0;i<n;i++) p[i]=item+i;//INITIALIZING POINTER ARRAY
     for(i=0;i<n;i++)cout<<*p[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;      }//SWAP
                                                             }//J
                                                        }//I
}
//end source code  


I know I need to modify my OP I put in where my old sort function was but I don't exactly get it. Can you help me just a little bit more. I really wanna understand how to do this and I think I am almost there. Lines 134-151 I think needs to be changed to apply to my program and I think I'll be there. Thank you for all of the feedback so far guys
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 04:02 PM

The quick fix is on line 17 and 134 an use the type float instead of int, since you are passing in an array of float's anyway.

Lines 17, and 134 should probably have the parameter be: float Values[], and change line 139 to use Values instead of item. You seem to have also forgotten to declare your array of pointers to float p.

Or you could change the signature to be:
void pointer_array_sort(float *p[], int n)


And get rid of line 139 entirely.
Was This Post Helpful? 0
  • +
  • -

#9 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 04:18 PM

Alright I changed Line 17 and 134 to float instead of int.
I changed the signature to the correct code
void pointer_array_sort(float *p[], int n)


Now do I get rid of:

for(i=0;i<n;i++) p[i]=item+i;//INITIALIZING POINTER ARRAY

or
for(i=0;i<n;i++)cout<<*p[i]<<" ";


Now my errors are:
1.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `int main()':
2.)43 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp cannot convert `float*' to `float**' for argument `1' to `void pointer_array_sort(float**)'
3.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `void pointer_array_sort(float**, int)':
4.)138 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `item' undeclared (first use this function)
5.) (Each undeclared identifier is reported only once for each function it appears in.)
6.)143 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp cannot convert `float*' to `int*' in assignment
7.)145 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp cannot convert `int*' to `float*' in assignment

I know I'm on the right track. Obviously there's a problem with the float somewhere.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: Sorting Netpay By The Pointers

Posted 03 August 2012 - 04:51 PM

You will have to move the line:
for(i=0;i<n;i++) p[i]=item+i;//INITIALIZING POINTER ARRAY


From inside the function, to someplay before you call the function. Basically you need to declare an array of pointers to float, and make each array element point to the corresponding netpay element. Then, obviously, pass this array of pointers to your function.
Was This Post Helpful? 0
  • +
  • -

#11 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Sorting Netpay By The Pointers

Posted 04 August 2012 - 11:40 AM

I reduced my errors to 5 of them. I still don't understand this float problem. Like I said this is all brand new to me and I just began programming for the first time 2 months ago. I understand you can't pass an array of floats to a function that accepts an array of floats but how do I fix this?

Errors:
1.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `void pointer_array_sort(float**, int)':
2.)139 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `item' undeclared (first use this function)
3.) (Each undeclared identifier is reported only once for each function it appears in.)
4.)144 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp cannot convert `float*' to `int*' in assignment
5.)146 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp cannot convert `int*' to `float*' in assignment

#include <iostream>
#include<fstream>//file input output stream
using namespace std; 

//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);
void sort(float[], int);
void pointer_array_sort(float*values);

int main(){
     const int MAXSIZE=50;  //for maximum of 100 employees
     
     //decleration of variables
     int n;
     long int id[MAXSIZE];
     int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
     int regularhours[MAXSIZE];
     float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
     overtimepay[MAXSIZE], grosspay[MAXSIZE];
     float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
     
     //function calls
     n=readalldata(id, 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, n);
     findtaxamount(grosspay, taxamount, taxrate, n);
     findnetpay(grosspay, netpay, taxamount, n);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
     pointer_array_sort(netpay);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      return 0;
}//MAIN
//function definitions
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
    ifstream fin("employee.txt");
    n=0;
    
    while(fin>>id[n]>>hoursworked[n]>>hourlyrate[n]) n++;
    fin.close();
    return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) overtimehours[i]=hoursworked[i]-40;
             else overtimehours[i]=0;
     }//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
     float overtimepay[], int n){
     for(int i=0; i<n; i++){
             overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
     }//FOR
}//FINDOVERTIMEPAY

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];
     }//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
                        float hourlyrate[], int n){
      for(int i=0; i<n; i++){
      regularpay[i] = regularhours[i]*hourlyrate[i];
      }//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
                        float grosspay[], int n){
                        for(int i=0; i<n; i++){
                        grosspay[i]=regularpay[i]+overtimepay[i];
        }//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
     for(int i=0; i<n; i++){
             if(grosspay[i]>4000.00) taxrate[i]=0.40;
             else if(grosspay[i]>3000.00) taxrate[i]=0.30;
             else if(grosspay[i]>1000.00) taxrate[i]=0.20;
             else taxrate[i]=0.10;
     }//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
                         float taxrate[], int n){
for(int i=0; i<n; i++){
        taxamount[i]=grosspay[i]*taxrate[i];
        }//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
     for(int i=0; i<n; i++){
          netpay[i]=grosspay[i]-taxamount[i];
     }//FOR
}//FINDNETPAY

void printalldata(long int id[], int hoursworked[], float hourlyrate[],
                       float overtimepay[], float grosspay[], float taxamount[],
                       float netpay[], int n){
                       float totalNetPay=0;
                       
                       int i=0;
                       cout<<"EMP ID"<<"\t"<<"HOURS"<<"\t"<<"RATE"<<"\t"
           <<"OVERPAY"<<"\t"<<"   GROSSPAY"<<"\t"<<"        TAX"<<"\t"
           <<"NETPAY"<<endl;
           
for(i; i<n; i++){
       totalNetPay +=netpay[i];
        cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
        <<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
        <<taxamount[i]<<"\t"<<netpay[i]<<endl;
        }//FOR
        cout<<"The net pay average of the employees are: "<<totalNetPay/i<<endl;
        system("PAUSE");
}//PRINTALLDATA

void pointer_array_sort(float*p[], int n)
{
  int i, j;
  int*temp;
     int sortedflag=0;
     for(i=0;i<n;i++) p[i]=item+i;//INITIALIZING POINTER ARRAY
     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;      }//SWAP
                                                             }//J
                                                        }//I
}
//end source code   

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4067
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Sorting Netpay By The Pointers

Posted 04 August 2012 - 11:59 AM

In the following snippet:
void pointer_array_sort(float*p[], int n)
{
   int i, j;
   int*temp;
   int sortedflag=0;
   for(i=0; i<n; i++) p[i]=item+i; //INITIALIZING POINTER ARRAY
   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;
         }//SWAP
      }//J
   }//I
}
 


What is the purpose of line 6? Shouldn't your array already be initialized to valid values?

Next since your parameter is a float*, your variable temp should also be a float*.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Sorting Netpay By The Pointers

Posted 04 August 2012 - 12:10 PM

#include <iostream>
#include<fstream>//file input output stream
using namespace std; 

//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);
void sort(float[], int);
void pointer_array_sort(float*values);

int main(){
     const int MAXSIZE=50;  //for maximum of 100 employees
     
     //decleration of variables
     int n;
     long int id[MAXSIZE];
     int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
     int regularhours[MAXSIZE];
     float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
     overtimepay[MAXSIZE], grosspay[MAXSIZE];
     float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
     
     //function calls
     n=readalldata(id, 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, n);
     findtaxamount(grosspay, taxamount, taxrate, n);
     findnetpay(grosspay, netpay, taxamount, n);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
     pointer_array_sort(netpay);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      return 0;
}//MAIN
//function definitions
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
    ifstream fin("employee.txt");
    n=0;
    
    while(fin>>id[n]>>hoursworked[n]>>hourlyrate[n]) n++;
    fin.close();
    return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) overtimehours[i]=hoursworked[i]-40;
             else overtimehours[i]=0;
     }//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
     float overtimepay[], int n){
     for(int i=0; i<n; i++){
             overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
     }//FOR
}//FINDOVERTIMEPAY

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];
     }//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
                        float hourlyrate[], int n){
      for(int i=0; i<n; i++){
      regularpay[i] = regularhours[i]*hourlyrate[i];
      }//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
                        float grosspay[], int n){
                        for(int i=0; i<n; i++){
                        grosspay[i]=regularpay[i]+overtimepay[i];
        }//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
     for(int i=0; i<n; i++){
             if(grosspay[i]>4000.00) taxrate[i]=0.40;
             else if(grosspay[i]>3000.00) taxrate[i]=0.30;
             else if(grosspay[i]>1000.00) taxrate[i]=0.20;
             else taxrate[i]=0.10;
     }//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
                         float taxrate[], int n){
for(int i=0; i<n; i++){
        taxamount[i]=grosspay[i]*taxrate[i];
        }//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
     for(int i=0; i<n; i++){
          netpay[i]=grosspay[i]-taxamount[i];
     }//FOR
}//FINDNETPAY

void printalldata(long int id[], int hoursworked[], float hourlyrate[],
                       float overtimepay[], float grosspay[], float taxamount[],
                       float netpay[], int n){
                       float totalNetPay=0;
                       
                       int i=0;
                       cout<<"EMP ID"<<"\t"<<"HOURS"<<"\t"<<"RATE"<<"\t"
           <<"OVERPAY"<<"\t"<<"   GROSSPAY"<<"\t"<<"        TAX"<<"\t"
           <<"NETPAY"<<endl;
           
for(i; i<n; i++){
       totalNetPay +=netpay[i];
        cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
        <<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
        <<taxamount[i]<<"\t"<<netpay[i]<<endl;
        }//FOR
        cout<<"The net pay average of the employees are: "<<totalNetPay/i<<endl;
        system("PAUSE");
}//PRINTALLDATA

void pointer_array_sort(float*p[], int n)
{
  int i, j;
  float*temp;
     int sortedflag=0;
     for(i=0;i<n;i++) p[i]=item+i;//INITIALIZING POINTER ARRAY
     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;      }//SWAP
                                                             }//J
                                                        }//I
}
//end source code   


Ok I fixed the float problem but my errors are:
1.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `void pointer_array_sort(float**, int)':
2.)139 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `item' undeclared (first use this function)
3.) (Each undeclared identifier is reported only once for each function it appears in.)
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 4067
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Sorting Netpay By The Pointers

Posted 04 August 2012 - 12:17 PM

Where is item declared, Hint it's not. What are you actually trying to accomplish with the following line:
for(i=0;i<n;i++) p[i]=item+i;//INITIALIZING POINTER ARRAY

As written it doesn't make any sense to me. Your variable p[i] should already have valid values, what are you actually trying to assign to that value? Better yet why are you trying to change the value in your sort routine. I recommend commenting out this line and see what happens.


Jim
Was This Post Helpful? 0
  • +
  • -

#15 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Sorting Netpay By The Pointers

Posted 04 August 2012 - 12:19 PM

Commenting out this line? What's that mean? Delete?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2