坪山手机网站建设新冠疫苗接种查询
一、题目
 用c语言的结构体,求4位学生成绩的平均分
 如图:

二、代码截图【带注释】

三、源代码【带注释】
#include <stdio.h>
 float aver();//声明平均分函数
 void printScore();//声明打印函数
//设置结构体,
struct student
 {
     int id;
     int score;
 } stu[4];
int main()
 {
     struct student stu[4]= {{1,80},{2,89},
     {3,78},{4,86}};
     printScore(stu);
     printf("\n4个学生的平均分是:%.2f",aver(stu));
 }
//打印4位学生的成绩
 void printScore(struct student stu[])
 {
     for(int i=0; i<4; i++)
     {
         printf("第%d位学生的成绩是:%d\n",(i+1),
         stu[i].score);
     }
 }
//设置平均分函数,求4位学生的平均分
 float aver(struct student stu[])
 {
     int sum=0;
     for(int i=0; i<4; i++)
     {
         sum=sum+stu[i].score;
     }
     return (float)sum/4;
 }
关注我, 每天分享编程知识
  
