某班有学生3人,开设五门课程,分别用函数实现如下操作: (1)求各门课程的平均分;(2)找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
C代码如下:
#include <stdio.h>
#include <string.h>
struct Student
{
int id;//学号
char name[20];
int score[5]; //成绩
};
int main()
{
int i,j;
int count = 0;
int flag[5];
struct Student stu[3];
memset(stu,0,sizeof(stu));
for (i=0;i<3;i++)
{
printf("请输入学生%d的学号:",i+1);
scanf("%d",&stu[i].id);
printf("请输入学生%d的姓名:",i+1);
scanf("%s",stu[i].name);
printf("请输入学生%d的五门课成绩:",i+1);
for(j=0;j<5;j++)
scanf("%d",&stu[i].score[j]);
}
//
for (i = 0;i<3;i++)
{
count = 0;
for(j=0;j<5;j++)
{
flag[j] = 0;
if (stu[i].score[j] < 60)
{
count++;
flag[j] = 1; //记录标记位
}
}
if(count >=2)//2门以上不及格
{
printf("%d",stu[i].id) ; //如果需要姓名,可以在这里加上
for(j = 0;j<5;j++)
{
if(flag[j] == 1)
printf(" %d",stu[i].score[j]);
}
printf("\n");
}
}
return 0;
}
C++代码如下:
#include <iostream>
using namespace std;
struct Student
{
int id;//学号
char name[20];
int score[5]; //成绩
};
int main()
{
int i,j;
int count = 0;
int flag[5];
struct Student stu[3];
memset(stu,0,sizeof(stu));
for (i=0;i<3;i++)
{
cout << "请输入学生" << i+1 << "的学号:";
cin >> stu[i].id;
cout << "请输入学生" << i+1 << "的姓名:";
cin >> stu[i].name;
cout << "请输入学生" << i+1 << "的五门课成绩:";
for(j=0;j<5;j++)
cin >> stu[i].score[j];
}
//
for (i = 0;i<3;i++)
{
count = 0;
for(j=0;j<5;j++)
{
flag[j] = 0;
if (stu[i].score[j] < 60)
{
count++;
flag[j] = 1; //记录标记位
}
}
if(count >=2)//2门以上不及格
{
cout << stu[i].id ; //如果需要姓名,可以在这里加上
for(j = 0;j<5;j++)
{
if(flag[j] == 1)
cout <<" " << stu[i].score[j];;
}
cout << endl;
}
}
return 0;
}