Sponsored
Sponsored
This approach involves iterating through each employee, checking if their employee_id is odd and their name does not start with 'M'. If both conditions are met, the bonus is set to their full salary; otherwise, it is set to 0. Loop through each employee and apply these checks systematically.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
1#include <stdio.h>
2#include <string.h>
3
4struct Employee {
5 int employee_id;
6 char name[100];
7 int salary;
8};
9
10struct Result {
11 int employee_id;
12 int bonus;
13};
14
15void calculate_bonus(struct Employee employees[], int n, struct Result results[]) {
16 for (int i = 0; i < n; i++) {
17 results[i].employee_id = employees[i].employee_id;
18 if (employees[i].employee_id % 2 == 1 && employees[i].name[0] != 'M') {
19 results[i].bonus = employees[i].salary;
20 } else {
21 results[i].bonus = 0;
22 }
23 }
24}
25
26int main(void) {
27 struct Employee employees[] = {
28 {2, "Meir", 3000},
29 {3, "Michael", 3800},
30 {7, "Addilyn", 7400},
31 {8, "Juan", 6100},
32 {9, "Kannon", 7700}
33 };
34 int n = sizeof(employees) / sizeof(employees[0]);
35 struct Result results[n];
36
37 calculate_bonus(employees, n, results);
38
39 printf("+-------------+-------+\n");
40 printf("| employee_id | bonus |\n");
41 printf("+-------------+-------+\n");
42 for (int i = 0; i < n; i++) {
43 printf("| %11d | %5d |\n", results[i].employee_id, results[i].bonus);
44 }
45 printf("+-------------+-------+\n");
46 return 0;
47}
This C program defines a struct for employees and then checks each employee's ID and name. If the ID is odd and name doesn't start with 'M', it assigns the employee's salary as the bonus. Results are stored in an array and printed.
This approach utilizes functional programming paradigms such as map/filter. It maps through each row of data, filters the rows based on the bonus eligibility criteria, and transforms them with the mapped values.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
1class Employee {
2 constructor(employeeId, name
In this JavaScript solution, map is used similarly to apply the transformation across the employee list. The solution calculates the bonus conditionally and stores it in the result array.