A little exercise of function returning pointer value -- dealing with student's score

Topic: there are 4 students and 5 courses in a class. Write three functions respectively to realize the following functions

1. Average the first course

2. Find out two or more failed students, output their student number, all scores and average scores

3. Find out students with an average score of more than 90 and all scores of more than 85

Idea: this problem is the consolidation of function knowledge points that return pointer values. The only thing to note is that when we want to process a two-dimensional array in a function, the actual parameter is the name of the two-dimensional array, and the formal parameter is the pointer variable pointing to the one-dimensional array. See the code for details

 

 1 #include<stdio.h>
 2 int main(){
 3     void avg1(int (*p)[5]);
 4     void fail2(int (*p)[5]);
 5     void dalao(int (*p)[5]);
 6     int a[4][5];
 7     
 8     for(int i=0;i<4;i++){
 9         printf("Please enter the %d Student scores:\n",i+1);
10         for(int j=0;j<5;j++){
11             scanf("%d",&a[i][j]);
12         }
13     }
14     avg1(a); 
15     printf("\n");
16     fail2(a);
17     printf("\n");
18     dalao(a);
19 }
20     //Seek the average score of the first course 
21 void avg1(int (*p)[5]){
22         int sum=0;
23         for(int i=0;i<4;i++){
24             sum=sum+*(*(p+i)+0);
25         }
26         printf("The average score of the first course is %.2f branch\n",(float)sum/4);
27     }
28     //Find out two students and output all their scores and average scores 
29 void fail2(int (*p)[5]){
30     int f;
31     int sum;
32     for(int i=0;i<4;i++){
33         f=0;sum=0;
34         for(int j=0;j<5;j++){
35             if(*(*(p+i)+j)<60) f++;
36             sum=sum+*(*(p+i)+j);
37         }
38         if(f>2){
39             printf("The first %d Two or more students failed, and their scores were as follows:\n",i+1);
40             for(int k=0;k<5;k++)
41               printf("%d ",*(*(p+i)+k));
42             printf(" The average score is %.2f",(float)sum/5);
43         }
44         printf("\n");
45     }
46 } 
47       //Find out the excellent students whose average score is above 90 and all scores are above 85 
48 void dalao(int (*p)[5]){
49     int flag[4]={0,0,0,0};
50     int sum,g,c=0;
51     for(int i=0;i<4;i++){
52         sum=0;g=0;
53         for(int j=0;j<5;j++){
54              sum=sum+*(*(p+i)+j);
55              if(*(*(p+i)+j)>85) g++;
56         }    
57          if(g==5) flag[i]=1;
58          if((sum/5)>90) flag[i]=1;
59     }
60     for(int k=0;k<4;k++){
61         if(flag[k]==1) printf("The first %d Students meet the standard",k+1);
62         else if(flag[k]==0) c++;
63     }
64     if(c==4) printf("No one meets the standard"); 
65 }

Operation result:

Tags: C

Posted on Tue, 05 May 2020 16:12:13 -0400 by system