From the list of participants, choose the one with the highest score.

#include <stdio.h> #include <math.h> #include <string.h> main(){ struct Rezultat { char fam[20]; char name[15]; int rez; } Rez; int rez1; int rez2; int rez3; struct Rezultat dig[100]; int i,j,n; struct Rezultat tmp; printf("Введите число участников: \n"); scanf("%d",&n); printf("Введите имена и результаты участников: \n"); for (i = 0; i < n; i++ ){ scanf("%s %s %d %d %d",Rez.fam,Rez.name,&rez1,&rez3,&rez3); Rez.rez = rez1 + rez2 + rez3; dig[i] = Rez; Rez.rez = 0; } for (i = n - 1; i >= 0; i-- ){ for (j = 0; j <= i; j++ ){ if (dig[j].rez > dig[i].rez){ tmp = dig[j]; dig[i] = dig[j]; dig[j] = tmp; } } } printf("\n%s %s %d",dig[n - 1].fam,dig[n - 1].name,dig[n - 1].rez); return 0; } 

c2.c: 24:21: warning: "rez2" may be used uninitialized in this function [-Wuninitialized] What does this mean? Shl: billiberd is displayed, why? Thanks in advance.

I redid it, maybe not rationally, but if you consider that I know the strings for two days, and with the structures for 3 hours, I think I managed:

 #include <stdio.h> #include <math.h> #include <string.h> main(){ struct Rezultat { char fam[20]; char name[15]; int rez; } Rez; int rez1; int rez2; int rez3; struct Rezultat dig[100]; int i,j,n,mm; struct Rezultat tmp; printf("Введите число участников: \n"); scanf("%d",&n); printf("Введите имена и результаты участников: \n"); for (i = 0; i < n; i++ ){ scanf("%s %s %d %d %d",Rez.fam,Rez.name,&rez1,&rez2,&rez3); Rez.rez = rez1 + rez2 + rez3; dig[i] = Rez; } for (i = n - 1; i >= 1; i-- ){ mm = 0; for (j = 0; j <= i; j++ ){ if (dig[j].rez > dig[i].rez){ mm = j; tmp = dig[i]; dig[i] = dig[mm]; dig[mm] = tmp; } } } for (i = 0; i < n - 1 ; i++){ if(dig[n - 1].rez == dig[i].rez ){ printf("\n%s %s\n",dig[i].fam,dig[i].name); } } printf("\n%s %s\n",dig[n - 1].fam,dig[n - 1].name); return 0; } 

The most rational thing I can do:

 #include <stdio.h> #include <math.h> #include <string.h> main(){ struct Rezultat { char fam[20]; char name[15]; int rez; } Rez; int rez1; int rez2; int rez3,rezmax; struct Rezultat dig[100]; int i,n; printf("Введите число участников: \n"); scanf("%d",&n); printf("Введите имена и результаты участников: \n"); for (i = 0; i < n; i++ ){ scanf("%s %s %d %d %d",Rez.fam,Rez.name,&rez1,&rez2,&rez3); Rez.rez = rez1 + rez2 + rez3; dig[i] = Rez; } rezmax = 0; /*поиск максимального результата*/ for (i = 0; i < n; i++ ){ if(dig[i].rez > rezmax){ rezmax = dig[i].rez; } } /*Поиск результатов,схожих с максимальным*/ for(i = 0 ; i < n ; i++){ if(rezmax == dig[i].rez){ printf("\n%s %s",dig[i].fam,dig[i].name); } } return 0; } 

Thanks, avp, 0xFFh!

  • one
    Typo in line 23 - fori1ton
  • one
    You have a typo in scanf (). Instead of & rez1, & rez2, & rez3, two times write & rez3. - avp
  • 2
    @rolton, why do you need sorting, if in the task described by you (as far as I remember) participants are required (or only one participant) with the maximum score? IMHO it is enough to find the maximum and remember the rest, equal to this maximum. - Regarding the storage of all data. The case, of course, is yours, but in my opinion it is ugly. It would be necessary to accumulate only the data that is needed and assume that the number of participants can be anything that is not known when the program is started. They are taken from the file. Here at the end of the file you will complete your processing. It will be right. - avp
  • You still do not have enough space in the fam and name arrays! In C, strings are terminated with a null character at the end, so the memory needs one character more than the length of the string. It is very important. - dzhioev

1 answer 1

If this is the whole task:

 Из списка участников,выбрать того,у которого самая высокая сумма баллов. 

what is your problem? If all participants (respectively, instances of the structure) are in the array, then you can simply run through all the structures in the array using a normal cycle, while comparing the "reZ" fields (the number of points, as I understand it).

This is one of the most primitive algorithms - the search algorithm in the array of the minimum value.

  • If several people have scored maximum results, then you need to bring them all. It’s nothing more rational than sorting and displaying maximum results, I didn’t come. Thanks for the advice. - rolton
  • 2
    @rolton, even if you stop at an array of structures in memory, then sorting it is clearly not rational . - The first pass looking for a maximum. The second type is all equal to the maximum. That's all. - avp
  • Oh, while I dealt with the structures, while one textbook looked through, while the other, while the question was closed here, everything normal flew out of my head. - rolton
  • @rolton, @avp correctly tells you: sorting the array only to search for the minimum / maximum in it is NOT RATIONAL. Moreover, in your case, sorting (by any algorithm) is more likely to “eat up” an order of magnitude more resources than a regular array run. - AseN
  • corrected the code, now it's better - rolton