summaryrefslogtreecommitdiff
path: root/lua/plugins/test.c
blob: bdba36733709f982dfe0856fc48552a4f271a1de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAX_NAME_LEN 50
#define ARRAY_SIZE 5

typedef enum {
    STUDENT,
    TEACHER,
    STAFF
} Role;

typedef struct {
    char name[MAX_NAME_LEN];
    int age;
    Role role;
    float grades[ARRAY_SIZE];
} Person;

// Function declarations
void print_person(const Person* p);
float average_grade(const Person* p);
Person* create_person(const char* name, int age, Role role);
void fill_random_grades(Person* p);
void print_all(Person** people, int count);
void free_all(Person** people, int count);
int find_oldest(Person** people, int count);

int main() {
    Person* people[ARRAY_SIZE];
    
    people[0] = create_person("Alice", 20, STUDENT);
    people[1] = create_person("Bob", 35, TEACHER);
    people[2] = create_person("Charlie", 28, STAFF);
    people[3] = create_person("Diana", 22, STUDENT);
    people[4] = create_person("Ethan", 40, TEACHER);

    for (int i = 0; i < ARRAY_SIZE; i++) {
        fill_random_grades(people[i]);
    }

    print_all(people, ARRAY_SIZE);

    int oldest_index = find_oldest(people, ARRAY_SIZE);
    printf("\nOldest person is: %s, Age: %d\n", people[oldest_index]->name, people[oldest_index]->age);

    free_all(people, ARRAY_SIZE);

    return 0;
}

void print_person(const Person* p) {
    const char* role_names[] = {"Student", "Teacher", "Staff"};
    printf("Name: %s\n", p->name);
    printf("Age: %d\n", p->age);
    printf("Role: %s\n", role_names[p->role]);
    printf("Grades: ");
    for (int i = 0; i < ARRAY_SIZE; i++) {
        printf("%.2f ", p->grades[i]);
    }
    printf("\nAverage Grade: %.2f\n\n", average_grade(p));
}

float average_grade(const Person* p) {
    float sum = 0;
    for (int i = 0; i < ARRAY_SIZE; i++) {
        sum += p->grades[i];
    }
    return sum / ARRAY_SIZE;
}

Person* create_person(const char* name, int age, Role role) {
    Person* p = (Person*)malloc(sizeof(Person));
    if (!p) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(1);
    }
    strncpy(p->name, name, MAX_NAME_LEN);
    p->age = age;
    p->role = role;
    memset(p->grades, 0, sizeof(p->grades));
    return p;
}

void fill_random_grades(Person* p) {
    for (int i = 0; i < ARRAY_SIZE; i++) {
        p->grades[i] = (float)(rand() % 101); // 0 to 100
    }
}

void print_all(Person** people, int count) {
    for (int i = 0; i < count; i++) {
        print_person(people[i]);
    }
}

void free_all(Person** people, int count) {
    for (int i = 0; i < count; i++) {
        free(people[i]);
    }
}

int find_oldest(Person** people, int count) {
    int oldest_index = 0;
    for (int i = 1; i < count; i++) {
        if (people[i]->age > people[oldest_index]->age) {
            oldest_index = i;
        }
    }
    return oldest_index;
}