C语言:程序填空:填写两个函数,使代码连接structure.h和CreateStruct.c后利用结构体计算每个学生的平均分并输出分数与其对应学生姓名

题目来源:大工慕课 链接
作者:Caleb Sung

题目要求

本题中要用到的结构体类型的定义放在31.structure.h文件中,31.CreateStruct.c文件里放的是函数crtstruct的定义,这个函数用来为结构体数组赋值,每个学生的8门成绩通过随机数得到并放在结构体的score成员数组里。本题需要完成如下任务:
(1)写一个函数ave()来求每个学生的平均分,并把平均分放到结构体的average成员里。
(2)写一个函数maxstd()在屏幕上输出平均分最高的所有学生的姓名和平均成绩(注意:平均分如有并列最高则将所有人都输出来)。

题目代码

31.结构体数组.c代码

#include <stdio.h>
#include <stdlib.h>
#include "31.CreateStruct.c"

//#include "31.structure.h"
/*         ERROR REPORT          */
/*If this line exists then the compiler reports
"31.structure.h(6) : error C2371: 'STD' : redefinition; different basic types"
So I search some blogs,say that delete this line helps*/

extern int crtstruct(STD std[],int n,int m);
/*************/
int ave(STD std[],int n);
void maxstd(STD std[],int n);
#define N 20
#define M 8
int main()
{
    int i,j;
    STD std[N];
    crtstruct(std,N,M);
    ave(std,N);
    for(i=0;i<N;i++)
    {
        printf("%s  %f  ",std[i].name,std[i].average);
        for(j=0;j<M;j++)
        {
            printf("%3d",std[i].score[j]);
        }
        putchar('\n');
    }
    maxstd(std,N);
    return 0;
}
int ave(STD std[],int n)
{
    // 编写函数完成题目要求。
        /*********begin*********/

        /**********end**********/
}
void maxstd(STD std[],int n)
{
    //编写函数完成题目要求。
        /*********begin*********/

        /**********end**********/
}

31.CreateStruct.c的代码

#include "31.structure.h"
#include <stdlib.h>
#include <time.h>
int crtstruct(STD std[],int n,int m)  //本函数用来为结构体数组赋值,n表示数组std的长度,m表示std->score 的长度
{
    int i,j;
    srand(time(NULL));
    for(i=0;i<n;i++)
    {
        (std+i)->name[0]='A'+i;
        (std+i)->name[1]='a'+i+1;
        (std+i)->name[2]='\0';
        for(j=0;j<m;j++)
        {
            std[i].score[j]=rand()%100;
        }
    }
    return 1;
}

31.structure.h的代码

typedef struct 
{
    char name[10];
    float average;
    int score[8];
}STD;

参考代码

int ave(STD std[],int n)
{
    // 编写函数完成题目要求。
        /*********begin*********/
    int i=0, j=0, sum;
    while(i < n){
        for(sum=0; j<M; sum+=std[i].score[j++]);
        std[i++].average = (float)(sum)/M;
    } 
    return 0;
        /**********end**********/
}
void maxstd(STD std[],int n)
{
    //编写函数完成题目要求。
        /*********begin*********/
    int i=0, j=0, sum[N], max=0;
    while(i < n){
        for(j=sum[i]=0; j<M; sum[i]+=std[i].score[j++]);
        i++;
    }
    for(i=0; i<n; i++)
        if(sum[i] > max)
            max = sum[i];
    for(i=0; i<n; i++)
        if(sum[i] == max)
            printf("%s  %f  \n", std[i].name, std[i].average);
        /**********end**********/
}

运行结果

每次运行的结果均会有所不同。

Ab  32.375000    7 43 48 12 63 35 42  9
Bc  61.875000   99 99 55 61 49  8 58 66
Cd  59.750000   60 21 23 47 93 64 83 87
De  67.750000   67 91 75 26 38 68 80 97
Ef  50.875000   44 65 48 20 98 19 77 36
Fg  55.875000   40  2 54 94 44 92 79 42
Gh  48.125000   28  2 93 76 27  0 79 80
Hi  59.500000   77 58 34 96 53 77 67 14
Ij  44.125000   38 23 78 59 29 89  9 28
Jk  56.000000   82 15 74 47 65  7 66 92
Kl  48.250000   71 11 74 47 85 53  8 37
Lm  42.625000   88 16 98 11 10 16 88 14
Mn  28.625000   36 41 55 45 32  4 13  3
No  52.000000   28 95 68 25 24 78 15 83
Op  45.625000    5 65 78 46 70 95  5  1
Pq  55.500000   88 96 35 62 32 40 11 80
Qr  60.000000   88 56 92 62 10 84 53 35
Rs  66.375000   83 13 50 99 92 51 70 73
St  61.500000   17 91 85 91 15 95 25 73
Tu  32.250000    4  6 45 74  4 26 24 75

附录:早先发布的错误版本的31.结构体数组.c代码

#include<stdio.h>
#include <stdlib.h>
/*************/
#include "structure.h"
extern int crtstruct(STD std[],int n,int m);
/*************/
int ave(STD std[],int n);
void maxstd(STD std[],int n);
#define N 20
#define M 8
int main()
{
    int i,j;
    STD std[N];
    crtstruct(std,N,M);
    ave(std,N);
    for(i=0;i<N;i++)
    {
        printf("%s  %f  ",std[i].name,std[i].average);
        for(j=0;j<M;j++)
        {
            printf("%3d",std[i].score[j]);
        }
        putchar('\n');
    }
    maxstd(std,N);
    return 0;
}
int ave(STD std[],int n)
{
    // 编写函数完成题目要求。
        /*********begin*********/

        /**********end**********/
}
void maxstd(STD std[],int n)
{
    //编写函数完成题目要求。
        /*********begin*********/

        /**********end**********/
}

这个程序在正常填写完函数之后将会报错,详细内容在前面正确版本代码中的Chinglish注释中。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值