Skip to main content

Employees with salary above 10,000

Short answer

A structure 'list' holds an array of 'eemp' and a count. Function 'highPaid' iterates all employees, returns a new list with only those whose salary > 10000. Sample run yields Sita, Gita, Maya, Anit, Suman.

Write a program to input name, post and salary of ten employees from main ( ) function and pass to structure type user defined function (arguments of this function should also be structure type). This function returns structure variable which keeps the record of only those employees whose salary is greater than 10,000. This modified record is also displayed from main ( ) function.

Design. A structure emp holds name, post and salary. A function must receive structures and return a structure, but one emp holds only one employee. So a second structure list is used which contains an array of 10 emp and a counter count. The function highPaid(struct list all) receives the structure of all employees, copies only those with salary > 10000 into a new list, and returns it. main() displays the returned (modified) record.

#include <stdio.h>
#define N 10

struct emp {
    char name[30];
    char post[20];
    float salary;
};

struct list {                 /* a structure that holds a group of records */
    struct emp e[N];
    int count;
};

/* structure argument, structure return value */
struct list highPaid(struct list all)
{
    struct list r;
    r.count = 0;
    for (int i = 0; i < all.count; i++)
        if (all.e[i].salary > 10000)
            r.e[r.count++] = all.e[i];
    return r;
}

int main(void)
{
    struct list all, res;
    all.count = N;
    for (int i = 0; i < N; i++) {
        printf("Employee %d (name post salary): ", i + 1);
        scanf("%29s %19s %f", all.e[i].name, all.e[i].post, &all.e[i].salary);
    }
    res = highPaid(all);
    printf("\nEmployees with salary above 10000\n");
    printf("%-10s %-10s %s\n", "Name", "Post", "Salary");
    for (int i = 0; i < res.count; i++)
        printf("%-10s %-10s %.2f\n", res.e[i].name, res.e[i].post, res.e[i].salary);
    return 0;
}

Sample run (salaries 9000, 45000, 7500, 22000, 10000, 31000, 8000, 90000, 15000, 9500)

Input (entered one after another): Ram Clerk 9000 Sita Manager 45000 Hari Peon 7500 Gita Officer 22000 Bibek Clerk 10000 Maya Accountant 31000 Kiran Peon 8000 Anita Director 90000 Suman Officer 15000 Rita Clerk 9500

Employees with salary above 10000
Name       Post       Salary
Sita       Manager    45000.00
Gita       Officer    22000.00
Maya       Accountant 31000.00
Anita      Director   90000.00
Suman      Officer    15000.00

The employee with salary exactly 10000 is not included, because the condition is "greater than".

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 8 questions →