结构体为什么求不出总分?
输入5名学生的学号、姓名和3门课程(programming、 database、 network)的成绩,存入一个结构体数组中。编写sumScore函数,其功能是计算学生3门课的总成绩,并存入结构体数组中。在主函数中输入学生信息,调用 sumScore函数,并输出学生的学号、姓名和总成绩信息。下面是我目前写出来的代码、
程序代码:
#include <stdio.h> struct courses { int programming; int database; int network; double score; }; struct student /*学生信息结构定义*/ { int num; /*学号*/ char name[10]; /*姓名*/ int sum; /*总分*/ struct courses sc[3]; }; int sumScore(struct student students[]); int main() { struct student students[5]; int i; for (i = 0; i < 3; i++) { printf("num:"); scanf("%d",&students[i].num); printf("name:"); scanf("%s",students[i].name); printf("programming score:"); scanf("%d",&students[i].sc[0].programming); printf("network score:"); scanf("%d",&students[i].sc[1].network); printf("database score:"); scanf("%d",&students[i].sc[2].database); } sumScore(students); printf("num\tname\tsum\n"); for (i = 0; i < 3; i++) { printf("%d\t%s\t%d\n",students[i].num,students[i].name,students[i].sum); } return 0; } int sumScore(struct student *students) { int i; for (i = 0; i < 3; i++) { students[i].sum=students[i].sc[0].database+students[i].sc[1].network+students[i].sc[2].programming; } }