Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems w/ function and structure

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello Everyone out there,

I am desperate for someone to help me with my C programming. I am taking beginning C language for the very first time. I have never taken a programming language before. For several sleepless nights I have been trying to work on this program. The question is: How do you create a function which uses the bubble-sort routine to sort the Employee's structure according to ascending salary? Can someone help me? I am terribly frustrated.

Signed "IamNewToC"


#include <stdio.h>
struct Employee
{
int age;
int years_employed;
long int salary;
};

main(){
struct Employee myEmployee[5] =
{
{ 25,1,36000},
{ 34,6,42000},
{ 36,10,44500},
{ 29, 4,38000},
{ 30, 7,41000}
};
}
 
#include <stdio.h>

#define MAX_EMPLOYEES 5

struct sEmployee {

int iAge;
int iYearsEmp;
long dSalary;


};

void Print_Records( const sEmployee myEmployee[],
int iRecordCount );
void BubbleSortBy_Income( sEmployee myEmployee[],
int iRecordCount );

int main() {


struct sEmployee myEmployee[MAX_EMPLOYEES] =
{

{ 25,1 ,12000 },
{ 34,6 ,10000 },
{ 36,10,44500 },
{ 29,4 ,12099 },
{ 30,7 ,99999 }


};


BubbleSortBy_Income( myEmployee,MAX_EMPLOYEES );
printf( &quot;Salary (highest to lowest) ->\n\n&quot; );
Print_Records( myEmployee,MAX_EMPLOYEES );

return 0;

}


void Print_Records( const sEmployee myEmployee[],
int iRecordCount ) {

for ( int i=0;i<iRecordCount;i++ )
{

printf( &quot;Employee #%d\n&quot;,i );
printf( &quot;Age : %d\n&quot;,myEmployee.iAge );
printf( &quot;Years : %d\n&quot;,myEmployee.iYearsEmp );
printf( &quot;Salary : %ld\n&quot;,myEmployee.dSalary );

}

}


void BubbleSortBy_Income( sEmployee myEmployee[],
int iRecordCount ) {

int iTemp = 0;
int iPass = 1;
int iIndex = 0;
int iExchanges = 1;
sEmployee sTempEmp;


while ( iPass < iRecordCount && iExchanges )
{

iExchanges = 0;

for ( int i=0;i<iRecordCount - iPass;i++ )
{

if( myEmployee.dSalary < myEmployee[i+1].dSalary )
{
sTempEmp = myEmployee;
myEmployee = myEmployee[i+1];
myEmployee[i+1] = sTempEmp;
iExchanges = 1;

}//End if.


}//End for.

++iPass;

}//End while.

}


Mike L.G.
mlg400@blazemail.com
 
//Use the above code if you save your file with a
//.cpp extension. But if you save if with a .c
//extension the compiler will complain so use ->

#include <stdio.h>

#define MAX_EMPLOYEES 5

typedef struct sEmployee {

int iAge;
int iYearsEmp;
long dSalary;


} Employee;

void Print_Records( const Employee myEmployee[],
int iRecordCount );
void BubbleSortBy_Income( Employee myEmployee[],
int iRecordCount );

int main() {


Employee myEmployee[MAX_EMPLOYEES] =
{

{ 25,1 ,12000 },
{ 34,6 ,10000 },
{ 36,10,44500 },
{ 29,4 ,12099 },
{ 30,7 ,99999 }


};


BubbleSortBy_Income( myEmployee,MAX_EMPLOYEES );
printf( &quot;Salary (highest to lowest) ->\n\n&quot; );
Print_Records( myEmployee,MAX_EMPLOYEES );

return 0;

}


void Print_Records( const Employee myEmployee[],
int iRecordCount ) {

int i;

for ( i=0;i<iRecordCount;i++ )
{

printf( &quot;Employee #%d\n&quot;,i );
printf( &quot;Age : %d\n&quot;,myEmployee.iAge );
printf( &quot;Years : %d\n&quot;,myEmployee.iYearsEmp );
printf( &quot;Salary : %ld\n&quot;,myEmployee.dSalary );

}

}


void BubbleSortBy_Income( Employee myEmployee[],
int iRecordCount ) {

int iTemp;
int iPass;
int iIndex;
int iExchanges;
int i;
Employee sTempEmp;

iTemp = 0;
iPass = 1;
iIndex = 0;
iExchanges = 1;
i = 0;


while ( iPass < iRecordCount && iExchanges )
{

iExchanges = 0;

for ( i=0;i<iRecordCount - iPass;i++ )
{

if( myEmployee.dSalary < myEmployee[i+1].dSalary )
{
sTempEmp = myEmployee;
myEmployee = myEmployee[i+1];
myEmployee[i+1] = sTempEmp;
iExchanges = 1;

}//End if.


}//End for.

++iPass;

}//End while.

}
Mike L.G.
mlg400@blazemail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top