Done
This commit is contained in:
57
Q1.C
57
Q1.C
@@ -1,43 +1,48 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
// Question 1
|
||||||
|
|
||||||
typedef struct Employee {
|
typedef struct Employee {
|
||||||
char name[50];
|
char name[50];
|
||||||
int age;
|
int age;
|
||||||
double salary;
|
double salary;
|
||||||
} Employee;
|
} Employee;
|
||||||
|
|
||||||
|
// Finds and returns employee with the highest salary in array
|
||||||
Employee find_highest_salary(Employee arr[], int len) {
|
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++) {
|
for(int i = 0; i < len; i++) {
|
||||||
if(arr[i].salary > highest_salary.salary) {
|
if(arr[i].salary > highest_salary.salary) {
|
||||||
highest_salary = arr[i];
|
highest_salary = arr[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return highest_salary;
|
return highest_salary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculates and returns average age of employees in arr
|
||||||
double average_age(Employee arr[], int len) {
|
double average_age(Employee arr[], int len) {
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
for(int i = 0; i < len; i++) {
|
for(int i = 0; i < len; i++) {
|
||||||
sum += arr[i].age;
|
sum += arr[i].age;
|
||||||
}
|
}
|
||||||
return sum / len;
|
return sum / len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Employee arr[5] = {
|
// Main function to test struct and the functions
|
||||||
{"George", 20, 500.25},
|
Employee arr[5] = {
|
||||||
{"Bob", 35, 450.75},
|
{"George", 20, 500.25},
|
||||||
{"Cane", 18, 69696969.69},
|
{"Bob", 35, 450.75},
|
||||||
{"Eva", 27, 125.25},
|
{"Cane", 18, 69696969.69},
|
||||||
{"Abagail", 30, 12.00}
|
{"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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user