C language / C + + common exercises Q & a collection of communication obstacles

C language / C + + common exercises Q & a collection (60): communication barriers

Beauty of procedure

In the Q & a session of CSDN, a little partner asked me for help, hoping to help him solve his confusion. He put forward the following questions and provided the corresponding C language implementation code.

subject

Enter the student number, name and grade of multiple students, and then output the name and student number of the student with the highest grade. Enter multiple sets of samples. Each set of examples contains an integer N, representing N students. Enter the student number, name and grade in the next N rows. If N = 0, the input ends

c language implementation:

#include<stdio.h>
#include<string.h>
struct stu_inf{
char name[10];
char num[20];
short score;
};
int main(void)
{
struct stu_inf s_max;
short num_max = 0;
int input_num;
char stu_name[10];
char stu_num[20];
int stu_score;
scanf("%d", &input_num);
getchar();
for (int i = 0; i < input_num; i++)
{
scanf("%s %s %d", stu_num, stu_name, &stu_score);
if (stu_score > num_max)
{
num_max = stu_score;
strcpy(s_max.name,stu_name);
strcpy(s_max.num, stu_num);
}
}
printf("%s %s",s_max.num,s_max.name);
}

How can I input multiple groups of data? When I encounter 0, the input ends? Thank you very much! Examples of input and output are as follows:

sample input 
2
1000 mingming 89
1001 lingling 90
3
1090 huanghuang 88
1076 zhouzhou 76
1003 xiaohong 87
0
 sample output 
1001 lingling
1090 huanghuang

Many other small partners on the network began to cross the sea with eight immortals and show their magic powers. One small partner quickly gave the answer and gave the corresponding operation results.

#include <stdio.h>

#include <string.h>

struct stu_inf
{
    char name[20];
    char num[20];
    short score;
};

int main(void)
{
    struct stu_inf s_max;
    short num_max = 0;
    int input_num;
    char stu_name[20];
    char stu_num[20];

    int stu_score;
    while (1)
    {
        num_max = 0;
        scanf("%d", &input_num);
        if(input_num == 0) break;
        getchar();

        for (int i = 0; i < input_num; i++)
        {
            scanf("%s %s %d", stu_num, stu_name, &stu_score);
            if (stu_score > num_max)
            {
                num_max = stu_score;
                strcpy(s_max.name, stu_name);
                strcpy(s_max.num, stu_num);
            }
        }
        printf("%s %s\n", s_max.num, s_max.name);
    }
}


The response of the small partner is:
”This is the same as the original code input and output. You still can't input multiple groups of data at one time“
"But I directly copy and paste it into a simple mobile phone compiler. Its input and output are the same as the original one."
"The same, that is, it can only input one group of data, which is the same as the requirement that" multiple groups of data are input at the same time, and when N=0, the output ends, and the results of all groups of data are output "

Another small online partner proposed another scheme, as follows:

use while loop
while(scanf("%d",&n),n!=0);

And give the corresponding code:

#include #include struct stu_inf{ char name[10]; char num[20]; short score; }; int main(void) { struct stu_inf s_max; short num_max = 0; int input_num; char stu_name[10]; char stu_num[20]; int stu_score; while(scanf("%d", &input_num) != EOF ) { for (int i = 0; i < input_num; i++) { scanf("%s %s %d", stu_num, stu_name, &stu_score); if (stu_score > num_max) { num_max = stu_score; strcpy(s_max.name,stu_name); strcpy(s_max.num, stu_num); } } } printf("%s %s",s_max.num,s_max.name); }

The response of the small partner is:
"I added it wrong. Did I add it in the wrong way?"

Here I am lost in thought. What's the problem? In fact, the above two little partners didn't answer much questions, so where are the questions?
I reread the topic again and carefully studied all the materials provided by my partner. Suddenly, the input and output example mentioned earlier attracted my attention:

sample input 
2
1000 mingming 89
1001 lingling 90
3
1090 huanghuang 88
1076 zhouzhou 76
1003 xiaohong 87
0
 sample output 
1001 lingling
1090 huanghuang

Oh, I feel that I suddenly understand. It turns out that we have been wrong all the time. The solution that the little partner wants is to input the number of students in a group, and then input the information of each student. The same as above, input the second group of students... The third group of students, and finally output the highest student information of the first group, the second group, the third group... The n group of students.

Thinking of this, I became cheerful. It took me about ten minutes to write the program as follows:

#include<stdio.h>
#include<string.h>

struct stu_inf{
char name[10];
char num[20];
short score;
};

int main(void)
{
    struct stu_inf s_max[100];
    short num_max = 0;
    int input_num;
    char stu_name[10];
    char stu_num[20];
    int stu_score;
    scanf("%d", &input_num);
    getchar();
 
    for (int i = 0; i < input_num; i++)
    {
        scanf("%s %s %d", stu_num, stu_name, &stu_score);
        if (stu_score > num_max)
        {
            num_max = stu_score;
            strcpy(s_max[i].name,stu_name);
            strcpy(s_max[i].num, stu_num);
        }
    }
    
    for (int i = 0; i < input_num; i++)
    {
        printf("%s %s\n",s_max[i].num,s_max[i].name);
    }
}

It was found that the data in the array always had problems in the input process of the above program, such as in the input

2
1000 mingming 89
1001 lingling 90
3

After breakpoint debugging, it is found that there is no problem, that is, when entering the following statement:

1090 huanghuang 88

Discovery s_ max[i]. The total data of num is always abnormal, either garbled or null. At first, I always thought it was caused by the time enter key. I kept adjusting the position of getchar(). When I found that it couldn't, I changed it to scanf("%c",c); It can't be found. Finally, it is simply replaced with setbuf(stdin,NULL); I found it was still not good, so I changed it to fflush(stdin). I was a little skeptical. I found that I had a problem with my understanding of scanf, setbuf and fflush. After a good toss, I finally found the problem. Friends, did you find it? The problem lies in "huanghuang", which is a string. Partners may ask, what's the problem with a string, and then look at the code char name[10]; The length of "huanghuang" is exactly 10. Scanf will automatically add '\ 0' to the end of the string. Because "huanghuang" exceeds the length, the read string is abnormal, so it is changed to char name[32]; Test run, input the test sample of the questioning partner, and output the perfect code as follows:

#include<stdio.h>
#include<string.h>
struct stu_inf{
char name[32];
char num[20];
short score;
};
int main(void)
{
    struct stu_inf s_max[100];
    short num_max = 0;
    int input_num;
    char stu_name[32];
    char stu_num[20];
    int stu_score, stu_count =0;
    while (1)
    {
        scanf("%d", &input_num);
        fflush(stdin);
        if (input_num == 0)
        {
            break;
        }

        num_max = 0;

        for (int i = 0; i < input_num; i++)
        {
            scanf("%s %s %d", stu_num, stu_name, &stu_score);
            fflush(stdin);
            if (stu_score > num_max)
            {
                num_max = stu_score;
                strcpy(s_max[stu_count].name,stu_name);
                strcpy(s_max[stu_count].num, stu_num);
            }
        }

        stu_count ++;
    }

    printf("\n");

    for (int i = 0; i < stu_count; i++)
    {
        printf("%s %s\n",s_max[i].num,s_max[i].name);
    }

    return 0;
}


After such a toss, I also deeply reflected. On the one hand, when we ask questions from our partners, we must first understand the real needs of our partners. Otherwise, no matter how much we do, we can't help our partners or even go the opposite way. On the other hand, when I can't get the correct results through repeated verification, we can re-examine the problem, Trying to explore the implied conditions and information, as well as the related incompatibility and disharmony, will help us understand and solve the problem.
Well, let's say so much today. I hope what I said can really help the young partners. For some young partners with good foundation, let's take it as a review. Your progress is my greatest happiness. Wish you happy to write code, happy to write code!!!

Keywords: C C++ Visual C++ Algorithm data structure

Added by gregambrose on Wed, 29 Dec 2021 03:43:37 +0200