#include <stdio.h>
#define N 10
struct student
{
int num;
char name[10];
float score[3];
};
void input(struct student *,int);
void print1(struct student);
void print(struct student *,int,float *);
int maxindex(float *,int);
int main(int argc, char *argv[])
{
struct student stu[N],*p;
int i;
float average[N],*f;
p = &stu;
f = average;
input(p,N);
printf("the source result:\n");
print(p,N,f);
p = &stu;
f = average;
i = maxindex(f,N);
printf("the max index %d,the details info is :\n",i);
print1(stu[i]);
system("pause");
return 0;
}
void input(struct student *p,int n)
{
int i;
for (i = 0; i < n; i++,p++)
{
printf("please input %d student info.\n",i + 1);
scanf("%d%s%f%f%f",&(p->num),&(p->name),&(p->score[0]),&(p->score[1]),&(p->score[2]));
}
}
void print(struct student *p,int n,float *f)
{
struct student *stu;
float average = 0;
int i;
for (stu = p; stu < p + n; stu++)
{
average = (stu->score[0] + stu->score[1] + stu->score[2]) / 3.0;
*f++ = average;
printf("num = %d ,name = %s , score[1] = %.2f ,score[2] = %.2f ,score[3] = %.2f ,average = %.2f\n",
stu->num,stu->name,stu->score[0],stu->score[1],stu->score[2],average);
}
}
void print1(struct student stu)
{
float average = 0;
average = (stu.score[0] + stu.score[1] + stu.score[2]) / 3.0;
printf("num = %d ,name = %s , score[1] = %.2f ,score[2] = %.2f ,score[3] = %.2f ,average = %.2f\n",
stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2],average);
}
int maxindex(float *p,int n)
{
int i,result = 0;
float sum = 0;
for (i = 0; i < n; i++)
{
if (*(p + i) > sum)
{
sum = *(p + i);
result = i;
}
}
return result;
}
|