记录第7章课后习题第3题: 题目:编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括num(学号),name(姓名),score[3](3门课成绩)。用主函数输入这些数据,用print函数输出这些数据。 声明结构体的同时定义一个数组stu【4】; 这里还是使用静态数组分配空间 动态用malloc和vector应该都可以,有机会再试吧(尬笑) 另:这里没必要用指针;
struct Student { int num; char name[50]; float score[3]; }stu[4]; //初始化五个学生信息; void print(Student stu[4]) { for(int i=0; i<5; i++) { cout<<stu[i].num<<" "; cout<<stu[i].name<<" "; for(int j=0; j<3; j++) { cout<<stu[i].score[j]<<" "; } cout<<"\n"; } cout<<endl; } int main() { cout<<"Please enter the information of five students"<<"\n"; for(int i=0; i<5; i++) { cin>>stu[i].num; cin>>stu[i].name; for(int j=0; j<3; j++) { cin>>stu[i].score[j]; } } cout<<"*******************"<<"\n"; print(stu); return 0; }