fourth assignment

Problem 6-1 Calculate the sum and difference of two numbers

1. Design ideas

(1)

Step 1: Read the question requirements.

Step 2: Write a program by setting variables according to the meaning of the question.

(2): Flowchart

2. Experimental code

 1 #include <stdio.h>
 2 #define MAXN 10
 3 
 4 struct student{
 5     int num;
 6     char name[20];
 7     int score;
 8     char grade;
 9 };
10 
11 int set_grade( struct student *p, int n );
12 
13 int main()
14 {   struct student stu[MAXN], *ptr;
15     int n, i, count;
16 
17     ptr = stu;
18     scanf("%d\n", &n);
19     for(i = 0; i < n; i++){
20        scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
21     } 
22    count = set_grade(ptr, n);
23    printf("The count for failed (<60): %d\n", count);
24    printf("The grades:\n"); 
25    for(i = 0; i < n; i++)
26        printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade);
27     return 0;
28 }
29 int set_grade( struct student *p, int n ){
30     int count = 0, i;
31     for(i = 0;i<n;i++,p++){
32         if(p->score<60){
33             p->grade = 'D';
34             count++;
35         }
36         else if((p->score<70)&&(p->score>=60)){
37             p->grade = 'C';
38         }
39         else if((p->score<85)&&(p->score>=70)){
40             p->grade = 'B';
41         }
42         else{
43             p->grade = 'A';
44         }
45     }
46     return count;
47 }

3. Problems encountered in the debugging process of this question and solutions

without

git address: https://coding.net/u/Drunktea/p/8.1/git/blob/master/8.1?public=true

 

6-2 Structure array sorted by total score

1. Design ideas

(1)

Step 1: Read the question requirements.

Step 2: Write a program by setting variables according to the meaning of the question.

(2) Flowchart

2. Experimental code

 1 #include <stdio.h>
 2 struct student                    
 3 {
 4 int num;
 5 char name[15];
 6 float score[3];
 7 float sum;
 8 };
 9 void calc(struct student *p,int n);     
10 void sort(struct student *p,int n);
11 int main()
12 {
13 struct student stu[5];
14 int i,j;
15 float f;
16 for(i=0;i<5;i++)
17 {
18     scanf("%d%s",&stu[i].num,stu[i].name);
19     for(j=0;j<3;j++)
20     { 
21         scanf("%f",&f);
22         stu[i].score[j]=f;
23     }
24 }
25 calc(stu,5);
26 sort(stu,5);
27 for(i=0;i<5;i++)
28 {
29     printf("%5d%15s",stu[i].num,stu[i].name);
30     printf("  %.1f  %.1f  %.1f  %.1f\n",stu[i].score[0],stu[i].score[1],stu[i].score[2], stu[i].sum);
31 }
32 return 0;
33 void calc(struct student *p,int n)
34 {
35     int i;
36     for(i=0;i<n;i++,p++)
37     {
38         p->sum = p->score[0]+p->score[1]+p->score[2];
39     }
40 }
41 
42 void sort(struct student *p,int n)
43 {
44     int i=0,j=0;
45     struct student t;
46     for(i=0;i<n;i++)
47     {
48         for(j=0;j<n-1;j++)
49         {
50             if(p[j].sum < p[j+1].sum)
51             {
52                 t      = p[j];
53                 p[j]   = p[j+1];
54                 p[j+1] = t;
55             }
56         }
57     }
58 }

3. Problems encountered in the debugging process of this question and solutions:

without

git address: https://coding.net/u/Drunktea/p/8.1/git/blob/master/8.2?public=true

What I have learned in the past two weeks:

1. The concept of structure

2. How to define structure type variables

3. Array of Structures

Please use tables and line graphs to present your line of code and time spent this week (4/9 8:00~4/26 8:00), blog word count and time spent

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324579575&siteId=291194637