성적 프로그램 구현
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int id;
char name[20];
float Kor;
float Eng;
float Math;
float Total;
float Ave;
}Student;
void middle(struct student student[])
{
int temp =0;
for(int i =0; i<2; i++)
{
for(int j=i; j<3; j++)
{
if(student[i].Kor>student[j].Kor)
{
temp = student[i].Kor;
student[i].Kor = student[j].Kor;
student[j].Kor = temp;
}
}
}
for(int i =0; i<2; i++)
{
for(int j=i; j<3; j++)
{
if(student[i].Eng>student[j].Eng)
{
temp = student[i].Eng;
student[i].Eng = student[j].Eng;
student[j].Eng = temp;
}
}
}
for(int i =0; i<2; i++)
{
for(int j=i; j<3; j++)
{
if(student[i].Math>student[j].Math)
{
temp = student[i].Math;
student[i].Math = student[j].Math;
student[j].Math = temp;
}
}
}
for(int i =0; i<2; i++)
{
for(int j=i; j<3; j++)
{
if(student[i].Ave>student[j].Ave)
{
temp = student[i].Ave;
student[i].Ave = student[j].Ave;
student[j].Ave = temp;
}
}
}
printf("중간값 %8.f %2.f %2.f %4.1f\n",student[1].Kor,student[1].Eng,student[1].Math,student[1].Ave);
}
int main()
{
Student student[3];
float KorAve=0;
float EngAve=0;
float MathAve=0;
float TotalAve=0;
int count[2] ={0,};//입력받을 구간
int divide = 0;
for(int i = 0; i<3; i++)
{
scanf("%d%s%f%f%f",&student[i].id,student[i].name,&student[i].Kor,&student[i].Eng,&student[i].Math);
student[i].Total =student[i].Kor+student[i].Eng+student[i].Math;
student[i].Ave = student[i].Total/3;
}
printf("학번 구간을 입력하시오: ");
scanf("%d %d",&count[0],&count[1]);
for(int i = 0; i<3; i++)
{
if(student[i].id>=count[0] && student[i].id<=count[1])
{
divide++;
KorAve +=student[i].Kor;
EngAve +=student[i].Eng;
MathAve += student[i].Math;
TotalAve += student[i].Ave;
}
}
KorAve /= divide;
EngAve /= divide;
MathAve /= divide;
TotalAve /= divide;
printf("학번 이름 국어 영어 수학 총점 평균\n");
for(int i = 0; i<3; i++)
{
if(student[i].id>=count[0] && student[i].id<=count[1])
{
printf("%2d %2s %3.f %3.f %3.f %5.f %2.1f\n",student[i].id,student[i].name,student[i].Kor,student[i].Eng,student[i].Math,student[i].Total,student[i].Ave);
}
}
printf("=========================================\n");
printf("전체 평균 %8.1f %2.1f %2.1f %4.1f\n",KorAve,EngAve,MathAve,TotalAve);
middle(student);
}