当前位置:网站首页>[c language] output the average score and the student data below or equal to the average score with the structure

[c language] output the average score and the student data below or equal to the average score with the structure

2022-06-11 18:18:00 Dancing Pierre

[C Language ] Use the structure to output the data of the average score and the students whose scores are lower than or equal to the average score

1、 subject

Student records consist of names and grades , Enter... In the main function 4 Data on students , Please write a function to calculate and return the average score output , And save and output the data of students whose scores are lower than or equal to the average through the structure pointer .

requirement :

  1. Use structure :
typedef struct Student 
{
    
	char name[20]; 
	int score; 
}Stu;
  1. Use sub functions :float StructAvg(Stu *a,Stu *b,int n,int *m)
  2. To output in the main function .

Input format : Input in sequence 4 Names and scores of students

Output format : Save and output the data of students whose scores are lower than or equal to the average

Examples :

Input :
KOBE 90
YAO 90
HC 80
JAMES 70
Output :
Avg=82.5
HC 80
JAMES 70

2、 Complete code

#include <stdio.h>
#define N 4
typedef struct Student {
    
    char name[20];
    int score;
}Stu;
float StructAvg(Stu* a, Stu* b, int n, int* m)
{
    
    int i, j = 0, sum = 0;
    float avg;
    for (i = 0; i < n; i++)
        sum += a[i].score;
    avg = sum * 1.0 / n;
    for (i = 0; i < n; i++)
        if (a[i].score < avg)
            b[j++] = a[i];
    *m = j;
    return avg;
}
int main()
{
    
    int i, j, n;
    float ave;
    struct Student s[N];
    for (i = 0; i < N; i++) {
    
        scanf("%s %d", s[i].name, &s[i].score);
    }
    struct Student h[N];
    ave = StructAvg(s, h, N, &n);
    printf("Avg=%.1f\n", ave);
    for (i = 0; i < n; i++)
        printf("%s %d\n", h[i].name, h[i].score);
    printf("\n");
}

3、 Screenshot

 Insert picture description here

原网站

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