- Asked 2/16
- 2073 Chaitra · 8 marks
- 2069 Chaitra · 6 marks
Sort employee structures by salary
Short answer
Read 10 employee records (name, age, salary) into an array of struct employee; sort by salary ascending using bubble sort (swap whole structures), then display. Sample sorted output: Hari 41 38000.00, Maya 33 39500.00, … Anita 38 70000.00.
Write a program in C to read name, age and salary of 10 different employees as the three members of a structure named as "employee". Sort this data in salary basis using user defined function and display sorted data from main function. (Variant: create an array of structure Employee with name and salary as members; pass it to a function which sorts in ascending order on the basis of salary and display the sorted array from main().)
Method. A structure employee with name, age and salary is declared and an array of 10 such structures is read in main(). The array is passed to sortBySalary(), which sorts it in ascending order of salary using bubble sort; whole structures are swapped with a temporary structure variable. The sorted data is displayed from main().
(For the variant with only name and salary, remove age; the same function works unchanged.)
#include <stdio.h>
#define N 10
struct employee {
char name[30];
int age;
float salary;
};
void sortBySalary(struct employee e[], int n)
{
struct employee t;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (e[j].salary > e[j + 1].salary) {
t = e[j];
e[j] = e[j + 1];
e[j + 1] = t;
}
}
int main(void)
{
struct employee e[N];
for (int i = 0; i < N; i++) {
printf("Employee %d (name age salary): ", i + 1);
scanf("%29s %d %f", e[i].name, &e[i].age, &e[i].salary);
}
sortBySalary(e, N);
printf("\n%-12s %-5s %s\n", "Name", "Age", "Salary");
for (int i = 0; i < N; i++)
printf("%-12s %-5d %.2f\n", e[i].name, e[i].age, e[i].salary);
return 0;
}
Sample run
Input (entered one after another): Ram 30 45000 Sita 25 52000 Hari 41 38000 Gita 29 61000 Bibek 35 47500 Maya 33 39500 Kiran 27 43000 Anita 38 70000 Suman 45 55000 Rita 31 41000
Name Age Salary
Hari 41 38000.00
Maya 33 39500.00
Rita 31 41000.00
Kiran 27 43000.00
Ram 30 45000.00
Bibek 35 47500.00
Sita 25 52000.00
Suman 45 55000.00
Gita 29 61000.00
Anita 38 70000.00
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Employees with salary above 10,000 · Computer Programming, chapter 8
- Display records sorted by name from file · Computer Programming, chapter 10
- Employee text file; sort into binary file · Computer Programming, chapter 10
- Read records until end; show nth record · Computer Programming, chapter 10 · asked 2×
- Switch versus user defined function · Computer Programming, chapter 5
- Sending structure members to a function · Computer Programming, chapter 8
- Write and display 10 employees using file · Object Oriented Programming, chapter 8
- Convert user-defined type to basic type · Object Oriented Programming, chapter 5
- Manager derived from person and employee · Object Oriented Programming, chapter 6
- RA: employees on project DBMS · Database Management System, chapter 3