This commit is contained in:
2024-10-22 21:44:21 -07:00
parent ab3b46420e
commit a7d959538a

57
Q1.C
View File

@@ -1,43 +1,48 @@
#include <stdio.h>
// Question 1
typedef struct Employee {
char name[50];
int age;
double salary;
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];
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;
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;
double sum = 0;
for(int i = 0; i < len; i++) {
sum += arr[i].age;
}
return sum / len;
}
int main() {
Employee arr[5] = {
{"George", 20, 500.25},
{"Bob", 35, 450.75},
{"Cane", 18, 69696969.69},
{"Eva", 27, 125.25},
{"Abagail", 30, 12.00}
};
// Main function to test struct and the functions
Employee arr[5] = {
{"George", 20, 500.25},
{"Bob", 35, 450.75},
{"Cane", 18, 69696969.69},
{"Eva", 27, 125.25},
{"Abagail", 30, 12.00}
};
printf("%0.02f\n", find_highest_salary(arr, 5).salary);
// Find highest salary
printf("%0.02f\n", find_highest_salary(arr, 5).salary);
printf("Average Employee Age: %0.02f\n", average_age(arr, 5));
// Find average age
printf("Average Employee Age: %0.02f\n", average_age(arr, 5));
return 0;
return 0;
}