qfedu-c-level/day12/homework/h6.c

37 lines
865 B
C
Raw Normal View History

2023-07-22 10:37:20 +08:00
// 编写一个程序,定义一个结构体表示员工的信息,包括姓名、工号和工资。编写一个函数,接受一个员工结构体数组和数组长度作为参数,并计算并返回所有员工的平均工资。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct employee_s
{
char name[32];
int eid;
double salary;
} EMPLOYEE;
double avgSalary(EMPLOYEE *emps, int n)
{
double avgS;
for (int i = 0; i < n; i++)
{
avgS += emps[i].salary / n;
}
return avgS;
}
int main()
{
int n; // 数组长度
printf("请输入数组长度: ");
scanf("%d", &n);
EMPLOYEE *employees = malloc(n * sizeof(EMPLOYEE));
employees[0].salary = 100;
employees[1].salary = 59;
printf("平均工资为: %.2lf", avgSalary(employees, n));
free(employees);
return 0;
}