使用数组:
#include <stdio.h>
#include <string.h>
int main()
{
int i,sum=0,min=100;
int a[8]={75,80,83,85,86,30};
char *MeanLow[4]={"人数","平均成绩","最低分数科目的成绩","最低分数的科目"};
char name[6][5]={"数学","物理","外语","政治","体育"};
printf("原始信息如下:\n");
for(i=0;i<5;i++)
printf("%s:%d\n",name[i],a[i]);
for(i=0;i<3;i++)
printf("%s:%d\n",MeanLow[i],a[i+5]);
printf("%s:\n",MeanLow[i]);
for(i=0;i<5;i++)
{
sum=sum+a[i];
if(min>a[i])
min=a[i];
}
a[6]=sum/5;
a[7]=min;
for(i=0;i<5;i++)
{
if(a[i]==min)
{
strcpy(name[5],name[i]);
break;
}
}
printf("统计结果如下:\n");
for(i=0;i<3;i++)
printf("%s:%d\n",MeanLow[i],a[i+5]);
printf("%s:%s\n",MeanLow[i],name[5]);
return 0;
}
使用内存:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i,*p,sum=0,min=100;
int a[8]={75,80,83,85,86,30};
char *MeanLow[4]={"人数","平均成绩","最低分数科目的成绩","最低分数的科目"};
char name[6][5]={"数学","物理","外语","政治","体育"};
printf("原始信息如下:\n");
for(i=0;i<5;i++)
printf("%s:%d\n",name[i],a[i]);
for(i=0;i<3;i++)
printf("%s:%d\n",MeanLow[i],a[i+5]);
printf("%s:\n",MeanLow[i]);
p=a;
for(i=0;i<5;i++)
{
sum=sum+p[i];
if(min>p[i])
min=p[i];
}
a[6]=sum/5;
a[7]=min;
p=(int *)malloc(8*sizeof(int));//分配内存空间
if(p==NULL)
{
printf("内存分配错误");
exit(1);
}
for(i=0;i<8;i++)
*(p+i)=a[i];
for(i=0;i<5;i++)
{
if(*(p+i)==min)
{
strcpy(name[5],name[i]);
break;
}
}
printf("统计结果如下:\n");
for(i=0;i<3;i++)
printf("%s:%d\n",MeanLow[i],*(p+i+5));
printf("%s:%s\n",MeanLow[3],name[5]);
free(p);
return 0;
}