当前位置:网站首页>Exercise 9-6 statistics of student scores by grade (20 points)

Exercise 9-6 statistics of student scores by grade (20 points)

2022-06-11 22:25:00 Xiaoyan y

According to the students' grades, the students' grades should be set , And count the number of failed simple function .

Function interface definition :

int set_grade( struct student *p, int n );

among p Is the pointer to the structure array of student information , The structure is defined as :

struct student{
    int num;
    char name[20];
    int score;
    char grade;
};

n Is the number of array elements . Student number num、 full name name And achievements score It's all stored .set_grade The function needs to be based on the student's grade score Set its level grade. Level setting :85-100 by A,70-84 by B,60-69 by C,0-59 by D. meanwhile ,set_grade You also need to return the number of people who failed .

Sample referee test procedure :

#include <stdio.h>
#define MAXN 10

struct student{
    int num;
    char name[20];
    int score;
    char grade;
};

int set_grade( struct student *p, int n );

int main()
{   struct student stu[MAXN], *ptr;
    int n, i, count;

    ptr = stu;
    scanf("%d\n", &n);
    for(i = 0; i < n; i++){
       scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
    } 
   count = set_grade(ptr, n);
   printf("The count for failed (<60): %d\n", count);
   printf("The grades:\n"); 
   for(i = 0; i < n; i++)
       printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade);
    return 0;
}

/* Your code will be embedded here */

sample input :

10
31001 annie 85
31002 bonny 75
31003 carol 70
31004 dan 84
31005 susan 90
31006 paul 69
31007 pam 60
31008 apple 50
31009 nancy 100
31010 bob 78

sample output :

The count for failed (<60): 1
The grades:
31001 annie A
31002 bonny B
31003 carol B
31004 dan B
31005 susan A
31006 paul C
31007 pam C
31008 apple D
31009 nancy A
31010 bob B

int set_grade( struct student *p, int n ){
    int count=0,i;
    for(i=0;i<n;i++){
        if(p->score>=85&&p->score<=100){
            p->grade='A';
        }else if(p->score>=70&&p->score<=84){
            p->grade='B';
        }else if(p->score>=60&&p->score<=69){
            p->grade='C';
        }else if(p->score>=0&&p->score<=59){
            count++;
            p->grade='D';
        }
        p++;
    }
    return count;
}

 

原网站

版权声明
本文为[Xiaoyan y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112218445022.html