Question
c programming How can I make this sort by name instead of salary, and how would I program the totals for the very bottom. I
c programming
How can I make this sort by name instead of salary, and how would I program the totals for the very bottom. I need a total salary, total amout of raises, and total amout
#include
struct employee { char name[50]; float salary; float rate; float raise; float new_salary; };
void sort(struct employee **arr, int N){ int i, j; for (i = 0; i < N - 1; i++){ // the previous i elements are placed for (j = 0; j < N - i - 1; j++) { // swap the two elements if (arr[j]->salary > arr[j+1]->salary) { char str[50]; // swap the names strcpy( str , arr[j]->name ); strcpy( arr[j]->name , arr[j+1]->name ); strcpy( arr[j+1]->name , str ); // swap the salary of two employees float temp = arr[j]->salary; arr[j]->salary = arr[j+1]->salary; arr[j+1]->salary = temp; // swap the rate of two employees temp = arr[j]->rate; arr[j]->rate = arr[j+1]->rate; arr[j+1]->rate = temp; // swap the raise of two employees temp = arr[j]->raise; arr[j]->raise = arr[j+1]->raise; arr[j+1]->raise = temp; // swap the salary of two employees temp = arr[j]->new_salary; arr[j]->new_salary = arr[j+1]->new_salary; arr[j+1]->new_salary = temp; } } } }
int main(){ int i; char name[50]; float salary; float rate; float raise; float new_salary; // create an array of size 7 struct employee **arr = ( struct employee ** )malloc( 7 * sizeof( struct employee * ) );
for( i = 0 ; i < 7 ; i++ ) arr[i] = ( struct employee * )malloc( 7 * sizeof( struct employee ) );
for( i = 0 ; i < 7 ; i++ ){ printf("Enter name : "); scanf("%s",name); memcpy(arr[i]->name,name,20); printf("%s ", arr[i]->name); printf("Enter salary : "); scanf("%f", &salary); arr[i]->salary=salary;
if(salary>=0 && salary<=29999){ arr[i]->rate=7; arr[i]->raise=(salary/100*7); arr[i]->new_salary=salary+(salary/100*7); } if(salary>=30000 && salary<=40000){ arr[i]->rate=5.5; arr[i]->raise=(salary/100*5.5); arr[i]->new_salary=salary+(salary/100*5.5); } if(salary>40000){ arr[i]->rate=4; arr[i]->raise=(salary/100*4); arr[i]->new_salary=salary+(salary/100*4); }}
if( i < 6 ) gets( arr[i]->name );
sort( arr , 7 );
printf(" NAME \t\tSALARY \t\tRATE %%\t\tRAISE\t\tNEW SALARY "); for( i = 0 ; i < 7 ; i++ ){ printf("%d %s \t\t%.2f \t%.3f \t\t%.2f \t%.2f ",i,arr[i]->name,arr[i]->salary,arr[i]->rate,arr[i]->raise,arr[i]->new_salary); } //printf(" TOTAL \t\t%.2f\t\t\t%.2f\t%.2f",total_sal,total_ras,total_new);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started