struct
#include<stdio.h>
#include<string.h>
typedef struct hs
{
char ten[50];
int dToan;
int dLy;
int dHoa;
}hocsinh;
void nhapmang(hocsinh a[],int n);
void inmang(hocsinh a[],int n);
int timmaxdtb(hocsinh a[],int n);
void sapxep(hocsinh a[],int n);
int main()
{
hocsinh a[100];
int n;
printf("nhap n=");
scanf("%d",&n);
nhapmang(a,n);
printf("mang vua nhap:
");
inmang(a,n);
printf("hoc sinh co diem trung binh lon nhat:%s
",a[timmaxdtb(a,n)].ten);
sapxep(a,n);
printf("mang sau khi sap xep:
");
inmang(a,n);
system("pause");
return 0;
}
void nhapmang(hocsinh a[],int n)
{
int i;
for(i=0;i<n;++i)
{
printf("nhap hoc sinh thu:%d
",i+1);
printf("nhap ten:");
scanf("%s",&a[i].ten);
printf("nhap diem toan:");
scanf("%d",&a[i].dToan);
printf("nhap diem ly:");
scanf("%d",&a[i].dLy);
printf("nhap diem hoa:");
scanf("%d",&a[i].dHoa);
}
}
void inmang(hocsinh a[],int n)
{
int i;
for(i=0;i<n;++i)
{
printf("Ten:%s
",a[i].ten);
printf("Diem Toan:%d
",a[i].dToan);
printf("Diem Ly:%d
",a[i].dLy);
printf("Diem Hoa:%d
",a[i].dHoa);
}
}
int timmaxdtb(hocsinh a[],int n)
{
int i;
int kq=0;
for(i=0;i<n;++i)
if((a[i].dToan+a[i].dLy+a[i].dHoa)>(a[kq].dToan+a[kq].dLy+a[kq].dHoa))
kq=i;
return kq;
}
void sapxep(hocsinh a[],int n)
{
int i,j;
hocsinh tam;
for(i=0;i<n;++i)
for(j=i+1;j<n;++j)
if((a[i].dToan+a[i].dLy+a[i].dHoa)<(a[j].dToan+a[j].dLy+a[j].dHoa))
{
tam=a[i];
a[i]=a[j];
a[j]=tam;
}
}
Bạn đang đọc truyện trên: Truyen247.Pro