Files
Lab2/Q1.C
2024-10-22 21:49:53 -07:00

49 lines
1.0 KiB
C

#include <stdio.h>
// Question 1
typedef struct Employee {
char name[50];
int age;
double salary;
} Employee;
// Finds and returns employee with the highest salary in array
Employee find_highest_salary(Employee arr[], int len) {
Employee highest_salary = arr[0];
for(int i = 0; i < len; i++) {
if(arr[i].salary > highest_salary.salary) {
highest_salary = arr[i];
}
}
return highest_salary;
}
// Calculates and returns average age of employees in arr
double average_age(Employee arr[], int len) {
double sum = 0;
for(int i = 0; i < len; i++) {
sum += arr[i].age;
}
return sum / len;
}
int main() {
// Main function to test struct and the functions
Employee arr[5] = {
{"George", 20, 500.25},
{"Bob", 35, 450.75},
{"Cane", 18, 70000.69},
{"Eva", 27, 125.25},
{"Abagail", 30, 12.00}
};
// Find highest salary
printf("%0.02f\n", find_highest_salary(arr, sizeof(arr) / sizeof(arr[0])).salary);
// Find average age
printf("Average Employee Age: %0.02f\n", average_age(arr, sizeof(arr) / sizeof(arr[0])));
return 0;
}