利用结构体 计算5个学生的平均成绩和不合格人数。
打印80-100分的学生的成绩及姓名。
- #include <stdio.h>
- struct student {
- int num;
- char *name;
- char *sex;
- float score;
- };
- int main(int argc, const char * argv[]) {
-
- struct student pp[5]={
- {101,"陈冠希","boy",45},
- {102,"张柏芝","girl",84},
- {103,"钟欣桐","girl",79},
- {104,"梁朝伟","boy",95},
- {105,"林心如","girl",99},
- };
-
- int sum=0;
- float avg=0;
- int j=0;
- for (int i=0; i<5; i++)
- {
-
- if (pp[i].score<80)
- {
- j=j+1;
- }
- else
- {
- printf("姓名:%s,成绩:%.2f\n",pp[i].name,pp[i].score);
-
- }
- sum=sum+pp[i].score;
- avg=sum/5;
-
- }
- printf("sum =%d,平均成绩为:%.3f,不合格人数为:%d\n",sum,avg,j);
复制代码 |
|